开发者

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

开发者 https://www.devze.com 2022-12-16 10:32 出处:网络
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. I

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

精彩评论

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

关注公众号