鉴于这两个模型:
class Conversation < ActiveRecord::Base
has_many :messages, :class_name => 'Message', inverse_of: 'conversation'
class Entity < Grape::Entity
expose :id, :title
expose :messages, :using => 'Message::Entity'
end
end
class Message < ActiveRecord::Base
belongs_to :conversation, :class_name => 'Conversation', inverse_of: 'messages'
class Entity < Grape::Entity
expose :id, :content
expose :conversation, :using => 'Conversation::Entity'
end
end
我需要将实体呈现为包含它们的关联的 json,因此:
这工作正常:
get do
@c = Conversation.find(1)
present @c, with: Conversation::Entity
end
虽然这不是:
get do
@m = Message.find(1)
present @m, with: Message::Entity
end
它给了我 null on conversation
:
{"id":1,"content":"#1 Lorem ipsum dolor sit amet","conversation":null}
所以看起来它不适用于 belongs_to 关联。
我需要做什么才能使其工作?