开发者

How to get custom attributes on .to_json method?

For example, i've this model:

class User < ActiveRecord::Base

  d开发者_开发百科ef foo
    bar
  end

end

When i do this: <%= User.all.to_json %>

I get this:

[{"user":{"created_at":"2011-06-07T17:40:21-03:00","login":"abcd", "password":"1234","updated_at":"2011-06-07T18:10:04-03:00"}}]

How can i get foo on this json too? Also, foo is an activerecord too..

Thanks


User.all.to_json(:methods => :foo)

It may also be useful to limit the attributes that are sent, you can do this with either the :only and :except options. e.g. It is unlikely you will want the password in this data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜