I’m working with a javascript library where I build comboboxes.
I have the requirement of build a combobox with full name of a person, so I mean name + surname.
Because in database those are 2 separate field (and in my model too), I would like to know if there is a fast way (instead of manually build all hash objects) to “simulate” the presence of an additional field in my model for JSON conversion, because this object must be returned as a JSON array where you can read *full_name* as a key.
Thanks for help
You can overwrite the to_json method in your model by passing
:methodsoptions and callsuper. This will callas_jsonversion of the super class with:methodsoptions so that it will serialize your model withfull_nameattributes.Inside your controller, you can simply
Check this document out if you want to know more options that can pass to
as_jsonmethod.http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html