I am using Mongoid as my backend and I am in need to return json with an “id” attribute instead of the default “_id” used by mongoid
for instance, I have now
[{
"_id": "4f2d8b971773eb18e6000001",
"name": "Scooter"
}, {
"_id": "4f2d8d9f1773eb18fd000001",
"name": "Coldplay"
}]
from a call to render:
format.json { render :json => @groups, only:[:name, :_id] }
but need,
[{
"id": "4f2d8b971773eb18e6000001",
"name": "Scooter"
}, {
"id": "4f2d8d9f1773eb18fd000001",
"name": "Coldplay"
}]
Any shortcuts?
Thank you!!
If you’re able to add an attribute accessor for
_idcalled justid, then this should be easily solved by overridingas_jsonin your model.Update: Made the override a bit more friendly to the parent method.