If your json object is actually an ActiveRecord object (or maybe more
correctly ActiveModel) and you always want these objects to render in
a particular way, then you can override as_json to achieve this. Then
using render json to serialise it will use use overridden as_json,
where you can include whichever bits (including calculated values)
that you desire.
If your json object is actually an ActiveRecord object (or maybe more
correctly ActiveModel) and you always want these objects to render in
a particular way, then you can override as_json to achieve this. Then
using render json to serialise it will use use overridden as_json,
where you can include whichever bits (including calculated values)
that you desire.
I in fact I already override two methods of mongoid to do this, but I still have some json strings to render, which is not very handy if I cannot use the “render :except” method.