defaults for to_json in Rails with :include

Posted by prateekdayal on Stack Overflow See other posts from Stack Overflow or by prateekdayal
Published on 2011-02-15T07:23:37Z Indexed on 2011/02/15 7:25 UTC
Read the original article Hit count: 113

Filed under:
|

Let us say I have a model Post which belongs to a User. To convert to json, I do something like this

@reply.to_json(:include => {:user => {:only => [:email, :id]}, 
               :only => [:title, :id])

However, I want to set some defaults for this so I don't have to specify :only everytime. I am trying to override as_json to accomplish this. When I add as_json in User model, it is called when I do @user.to_json but when user is included in @reply.to_json, my overriden as_json for User is ignored.

How do I make this work?

Thanks

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about JSON