我有 Trip 模型,其目的地定义如下:
class Trip < ActiveRecord::Base
...
has_and_belongs_to_many :destinations, join_table: :trips_destinations
...
end
我想要做的是公开包含相关目的地的旅行信息。我为目的地定义了这个响应实体:
module Services
module Trips
class DestinationResponseEntity < Grape::Entity
expose :id
expose :name
end
end
end
旅行目的地实体是这样的:
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
我以这种方式呈现结果:
present trip, :with => Trips::TripResponseEntity
但是服务的响应总是返回一个空的目标数组。
[{"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"]
在控制台中,我可以正确查看与旅行相关的所有目的地。任何可能导致问题的线索都非常感谢。