As Aswin above :methods will allow you to return a specific model method / function as a json attribute if you have a complex association will do the trick since it will add functions to the existing model / associations: D will work like a charm if you donβt want to override as_json
Check out this code and notice how I use :methods , as well as :include [N + Query is not even an option;)]
render json: @YOUR_MODEL.to_json(:methods => [:method_1, :method_2], :include => [:company, :surveys, :customer => {:include => [:user]}])
The Overwritting as_json function will be more complicated in this scenario (especially because you must add the interlocutors :include manually: / def as_json(options = { }) end
d1jhoni1b Nov 29 '17 at 19:34 on 2017-11-29 19:34
source share