i have Trip model which has destinations defined like that:
class Trip < ActiveRecord::Base
...
has_and_belongs_to_many :destinations, join_table: :trips_destinations
...
end
What I want to do is to expose the trip information included the associated destinations. I defined this response entity for the destinations:
module Services
module Trips
class DestinationResponseEntity < Grape::Entity
expose :id
expose :name
end
end
end
And the trip destination entity is this:
module Services
module Trips
class TripResponseEntity < Grape::Entity
expose :id
expose :title
expose :duration
expose :total_price
expose :description
expose :destinations, using: Trips::DestinationResponseEntity
expose :photo
end
end
end
I'm presenting the result in that way:
present trip, :with => Trips::TripResponseEntity
But the response of the service returns always an empty destination array.
[{"id":3,"title":"Islandhopping in Thailand","duration":14,"total_price":3450,"description":"Relax under swaying palm trees then jump into crystal-clear waters",**"destinations":[]**,"photo":"http://s3.amazonaws.com/ntradadevelopment/images/trips/3/original/thailand.jpeg"]
In the console I can see all the destinations associated with the trip properly. Any clue of what could be causing the issue is really appreciated.
What you're doing with Grape::Entity looks correct to me.
I encountered a similar issue that turned out to be a problem with my has_and_belongs_to_many relationship not being defined correctly so you might need to check that again.
In particular, you seem to be overriding the default naming convention of the join table. Could it be tripping up Grape?