Return alias field via json using mongoid
mongodb, mongoid
Solution
Well you can try overriding serializable_hash method. Just add something like this in your model.
def serializable_hash(options)
original_hash = super(options)
Hash[original_hash.map {|k, v| [self.aliased_fields.invert[k] || k , v] }]
end
Problem
I am using mongoid(2.6.0) with its alias and this is how my model field looks like ``` class Place include Mongoid::Document field :n, :as => :name, :type => String .... ``` Now I have a controller which finds a place and return the object as json ``` @places = Place.find({some query}) respond_to do |format| format.json { render json: @places } end ``` Now when I do ``` JSON.parse(response.body) ``` My response contains the field as "n" and not as "name". Is there a way I can ask mongoid to return me the alias name and not the actual name?