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
精彩评论