Rails 5 render JSON with object including decorated association

1.1k Views Asked by At

My Rails 5 app has a Ride model and a Waypoint model, where one Ride has many Waypoints.

Also I have a waypoint decorator, that decorates some of the waypoint fields.

In my Rails app I want to return a list of rides and all there nested waypoints.

@api_v1_rides = Ride.all
render json: {
      status: 200,
      rides: @api_v1_rides
}.to_json( :include => [:waypoints])

Works great.

But I want to return the decorated waypoints, so what I tried was:

@api_v1_rides = Ride.all
render json: {
      status: 200,
      rides: @api_v1_rides
}.to_json( :include => [:waypoints.decorate])

But this gives me an error saying that decorate is not a function of waypoints. I am using the Drape Gem to decorate and it works just fine in other parts of the app.

1

There are 1 best solutions below

0
rudydydy On

for complex json structure i would recommend you to use this gem active model serializer