Override as_json in Rails
When we call as_json on ActiveRecord model object, it returns json of attributes on model by calling as_json. We can override as_json method and add extra attributes, rename keys, add values of methods on object to the json returned.
Let’s take an example to understand this.
Let’s say we have a model Post
as given below.
The post has values as given below.
Now, calling as_json returns hash as given below.
Let’s look at the source code of as_json
method.
This method works as given below.
- It checks, if
options
passed has keyroot
. - If
root
key is passed orActiveRecord::Base.include_root_in_json = true
is set totrue
, it will include root node (model name) in resultant hash / json. - At the end it calls
serializable_hash
with options that uses ActiveModel::Serializer to serialize the model attributes.
Override as_json method on ActiveRecord model object
Let’s say, we want to add user_name
as well in the response
corresponding to user_id
of the post
.
We can achive this by overriding as_json
key in the response.
- Here, we have overriden
as_json
method - Called
super(options)
to fetch json that would have been returned otherwise. - Merged
user_name
key-value in the resultant hash / json.
Rename key on as_json response of ActiveRecord model object
Similarly, we can rename key from the response of
as_json
, by overriding the method.
Here, we have renamed the title
key in the resultant response
to post_title
.
When json generation gets complicate with overriding as_json
method
on ActiveRecord model object,
it is better to use jbuilder to generate json response for APIs
References
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox