In my Item controller, I wish to add a transient (i.e. non-persistent) attribute to my model object before rendering it as JSON.
def show
@item = Item.find(params[:id])
@item.comment = "some comment"
render :json => @item
end
My Item class looks like this:
class Item < ActiveRecord::Base
attr_accessor :comment
@comment
end
My problem is that the comment instance variable is not being rendered in the JSON. Everything that is persistent appears in the JSON output. Do I need to override the to_json method in order to make this work? Or is there an easier way to ensure that the comment is rendered in the JSON output?
Thanks for your help.
————– Update
This is the solution that has evolved out of Chubas suggestion. Overriding the to_json method on Item:
def to_json(options = {})
options[:methods] = :comment;
super(options)
end
Would love to know if this concurs with your thoughts, Chubas.
If you want more control over what to render, rather than creating the json object in the controller (bad design), override the method
as_jsonin the model, calling super and using themethodsoption to include:comment. You can see the default behavior here