Association Activerecord as JSON with Grapes

Is there an easy way to return activerecord models with associations like JSON using the Grape microframe?

get 'users' do
  User.includes(:address)
end

This fragment does not work, but User.includes(:address).to_json(include: :address)will be encoded twice as JSON. (Using the method to_jsonalone does not feel anyway)

+4
source share
2 answers

You can use instead #as_json.

So you can do

User.includes(:address).as_json(include: :address)

and this gives a hash instead of a json string.

+7
source

This seems like a good grape-active_model_serializers plugin app .

JSON , , , / .

+3

All Articles