开发者

Rails: How can I render multiple objects to JSON?

I am trying to render multiple objects as JSON. This is my render call:

render :json => {:开发者_StackOverflowwidget => @widget.to_json(:include => :foo), 
                 :updated => Time.now.to_i}

I have to use to_json because of the include, and the addition updated so I know when the last call was made. The problem is that the to_json is rendered as a String instead of the object structure of the widget.

How do I get the full object structure of the widget and the updated information?


Move the :include => :foo into your Widget model.

class Widget < ActiveRecord::Base
  def as_json(options = {})
    super options.merge(:include => :foo)
  end
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜