Organization
和Link
通过Node
关联。Organization
has_many :nodes
has_many :links, through: :nodes, source: :where_first_links
Node
belongs_to :organization
has_many :where_first_links, class_name: "Link",
foreign_key: "first_node_id"
has_many :where_second_links, class_name: "Link",
foreign_key: "second_node_id"
Link
belongs_to :first_node, class_name: "Node"
belongs_to :second_node, class_name: "Node"
问题:如何将
Link
关联回Organization
?我试过下面这一行,但似乎不起作用(ArgumentError: Unknown key: :through.
):belongs_to :organization,
through: :first_node,
source: :where_first_links,
inverse_of: :links
最佳答案
通过密钥不支持属于关联
你应该用has_one联想
has_one :first_node_organization,
through: :first_node,
class_name: 'Organization',
source: :organization
关于ruby-on-rails - 如何创建“通过”关联?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35083313/