in one of my Rails 3 controllers I need to return jSON response with only couple of attributes on the model, so I used
respond_with foo_list :only => [:id, :name], :methods => []
but still in the resposne I see more than just these 2 attributes, I noticed that those undesirable attributes are actually dynamic attributes generated from the query select function:
foo_list = Foo.select("count(bar) as dattr1, sum(adafsd) as dattr2")....
is there a way to work around this? or I have to build the hash myself?
thanks!
The conventional way to do this is to use the #as_json method on ActiveRecord that automatically converts an object to it’s json representation.
You can override it with whatever attributes you want: