Is there a simple way to return activerecord models with associations as JSON using the Grape microframework?
get 'users' do
User.includes(:address)
end
This snippet isn't working and User.includes(:address).to_json(include: :address)
will get encoded twice as JSON. (To use the to_json
method on my own doesn't feel right anyway)
You might want to use #as_json
instead.
So you can do
User.includes(:address).as_json(include: :address)
and that gives you a hash instead of a json string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With