What is the best way if i would like to only return :id and :name fields in JSON
So far i have:
format.json { render :json => @contacts.map(&:attributes) , :only => ["id"]}
But the “name” attribute does not work in the :only section, since it is not a column in the database (it is defined in the model as firstname + lastname)
Thanks!
You can pass
:methodsto to_json / as_jsonAlternatively you can just build up a hash manually
See: http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json