1

:inverse_to 不适用于我的复杂案例

class Communication::Message < ActiveRecord::Base
  belongs_to :conversation_for_proposal, {
    :class_name  => 'Communication::Conversation::ForProposal',
    :foreign_key => :conversation_id,
    :inverse_of  => :messages_for_proposal
  }
end

class Communication::Conversation::ForProposal < ActiveRecord::Base
  has_many :messages_for_proposal, {
    :class_name  => 'Communication::Message',
    :foreign_key => :conversation_id,
    :inverse_of  => :conversation_for_proposal
  }
end

问题:Communication::Conversation::ForProposal 不知道消息

Communication::Conversation::ForProposal.new.messages_for_proposal.build.conversation_for_proposal # => ok

Communication::Message.new.build_conversation_for_proposal.messages_for_proposal # => []

思考:

Communication::Message.reflect_on_association(:conversation_for_proposal).active_record
# => Communication::Message

Communication::Message.reflect_on_association(:conversation_for_proposal).inverse_of.active_record
# => Communication::Conversation::ForProposal

我想念什么?

4

1 回答 1

1

您需要指定 :inverse_of 选项。

class Post < AR::Base
  has_many :comments, :inverse_of => :post
end

class Comment < AR::Base
  belongs_to :post, :inverse_of => :comments
end
于 2012-04-25T17:01:07.880 回答