我有兩個模型,Conversation
和Message
,和一個關注Conversible
。 A Conversible
有一個Conversation
和一個Conversation
有很多Message
的。我想設置Conversible
,所以我可以在我的Conversible
上撥打messages
,它將返回Message
的Conversation
。這是我到目前爲止:has_many通過has_one
module Conversible
extend ActiveSupport::Concern
included do
has_one :conversation, as: :conversible dependent: :destroy
has_many :messages, through: :conversation
end
end
class Message < ActiveRecord::Base
belongs_to :conversation
end
class Conversation < ActiveRecord::Base
belongs_to :conversible, polymorphic: true
has_many :messages, dependent: :destroy
end
不幸的是,這是行不通的。我可以撥打conversible.messages
,但它總是返回一個空關係,即使conversible.conversation.messages
返回與其Message
的關係。
我錯過了什麼?
你在哪裏包含模塊'Conversible'? – IS04 2014-09-04 19:14:23
我們有四個其他模型,包括'可轉換的'。例如,有一個「目標」模型應該有一個「對話」。我離開他們試圖保持簡單的問題。 – LandonSchropp 2014-09-04 23:29:35