Hi,
I'm using to_json, and including associations. However, the resulting json object includes all of the methods for the associated objects, even when I ask it to exclude methods.
I've tried these ways of doing it:
render :json => @entries.to_json(:include => {:labels => {:only => [:label_id, :name], :methods => []}})
render :json => @entries.to_json(:include => {:labels => {:only => [:label_id, :name], :methods => :none}})
render :json => @entries.to_json(:methods => [], :include => {:labels => {:only => [:label_id, :name], :methods => []}})
And I get the following object:
"entry": {
"id" : "1",
"other property of entry" : "value",
...
"labels" : {
"0" : {
"name" : "animals",
"label_id" : "2",
},
"1" : {
"name" : "furry animals",
"label_id" : "5",
},
"_each" : "... method properties",
"_reverse" "... method properties",
etc...
}
}
So each json object for "entry" is created correctly, the associated labels are included, but I can't get it to omit the methods within "labels".
Any ideas for how to do this correctly?
Thanks.
EDIT:
@nirvdrum, thanks for the suggestion. That doesn't fix it unfortunately.
I've added this to the model
def as_json(options={})
super(:include => {:labels => {:only => [:label_id, :name]} } )
end
And the controller does this:
render :json => @entries
And the result is the same. Any other suggestions would be appreciated.