【发布时间】:2013-08-21 17:26:53
【问题描述】:
在下面的例子中,在 Chlid 类中使用 belongs_to :mother 和 has_one :mother 有什么区别?我一直在阅读有关这方面的 Rails 文档,除了阅读它所涉及的语义之外,我看不出任何一个会产生什么不同。
据我所知,各种关联为每个类添加了额外的方法,但我无法找到文档来列出每个关联的方法是什么以及它们的作用。
class BiologicalMother < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :biological_mother
end
【问题讨论】:
-
这是“在 belongs_to 和 has_one 之间选择”的官方文档:guides.rubyonrails.org/…,上面写着:
"The distinction is in where you place the foreign key (it goes on the table for the class declaring the belongs_to association)"
标签: ruby-on-rails associations