开发者

Is there any way to non-verbosely output all dependencies using to_json?

In Rails apps, I find myself placing a lot of nested开发者_运维百科 :include => statements in my JSON rendering code to include objects with relationships(direct and indirect) to the root object. Is there any way to get to_json(or a similar method) to automatically include all related objects without explicitly specifying them?


Sure, override the #to_json method:

class Post < ActiveRecord::Base
   def to_json(options={})
     super(options.merge(:include => :comments, :methods => [:arbitrary_field]))
   end
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜