[英]In Rails, what is the difference using “has_many with belongs_to” vs “has_many with has_one”?
For example, in 例如,在
class Student < ActiveRecord::Base
has_many :awards
end
class Awards < ActiveRecord::Base
belongs_to :student
end
the above should be the correct usage, but what if we use 以上应该是正确的用法,但是如果我们使用的话
class Student < ActiveRecord::Base
has_many :awards
end
class Awards < ActiveRecord::Base
has_one :student
end
doesn't the above also make possible student.awards
as an array of Award objects, and award.student
as a Student object which is the recipient of the award, so works the same way as the method at the top of post? 以上是不是也可以将
student.awards
作为一系列Award对象,并将award.student
作为学生对象作为奖励的接收者,所以工作方式与帖子顶部的方法相同?
has_one
is used in case of a one-to-one relationship, not in one-to-many relationships. has_one
用于一对一关系,而不是一对多关系。
Correct use of has_one
: 正确使用
has_one
:
class Student < ActiveRecord::Base
has_one :id_card
end
class IdCard < ActiveRecord::Base
belongs_to :student
end
The two examples are not equivalent. 这两个例子并不相同。
has_many
and belongs_to
work as a pair where there is a 'many to one' relationship. has_many
和belongs_to
作为一对有一个“多对一”的关系工作。
In the database this would look like: 在数据库中,这将是:
**Students**
Name
Email
...
**Awards**
Name
student_id <-- !IMPORTANT!
...
Each Student
has many awards hence has_many :awards
Each Award
'belongs to' a Student
hence belongs_to :student
每个
Student
都有很多奖项因此has_many :awards
每个Award
'属于' Student
因此belongs_to :student
Notice that the belongs_to
is applied to the table with the foreign key student_id
. 请注意,
belongs_to
应用于具有外键student_id
的表。 This is important. 这个很重要。
OK - so what happens where there is a 'one to one' relationship? 好的 - 那么在存在“一对一”关系的情况下会发生什么?
If each student could only have a single award then the database tables could look exactly the same but the model should not be able to return a collection of items. 如果每个学生只能获得一个奖励,那么数据库表看起来可能完全相同,但模型不应该返回一个项目集合。
This is where we need the has_one
declaration. 这是我们需要
has_one
声明的地方。 This would be applied to the Student
model in this case. 在这种情况下,这将应用于
Student
模型。 Why? 为什么? Because the relationship is the same in both directions but Active Record needs to know where to find the foreign key.
因为两个方向的关系相同,但Active Record需要知道在哪里找到外键。
If the database tables were the other way around with each Student
having an award_id
then the Student
would get the belongs_to
and the Award
would get the has_one
. 如果数据库表是相反的,每个
Student
都有一个award_id
那么Student
将获得belongs_to
, Award
将获得has_one
。
Hope that makes it clear? 希望说清楚吗?
It does seem a little odd to be that a student could 'belong to' an award if you use natural language. 如果你使用自然语言,学生可以“属于”奖励似乎有点奇怪。 But that is how the rails active record domain specific language is written.
但这就是rails活动记录域特定语言的编写方式。
It gets even more unnatural sounding when you look at the 'many to many' relationship with 'has_many_and_belongs_to'. 当你看到'has_many_and_belongs_to'与'多对多'的关系时,它会变得更加不自然。 Here there is a joining table which site between your main tables like
这里有一个连接表,你的主表之间的网站就像
students_awards
student_id
award_id
In this situation neither the Students
nor the Awards
table have a foreign key but both will carry the has_many_and_belongs_to :other_table
declaration. 在这种情况下,
Students
和Awards
表都没有外键,但两者都带有has_many_and_belongs_to :other_table
声明。 Both tables are able to join to multiple rows of the other. 两个表都能够连接到另一个表的多个行。 Each
Student
can have more than one Award
. 每位
Student
都可以获得多个Award
。 Each Award
can be applied to many Students
. 每个
Award
都适用于许多Students
。
The has_one
declaration is only used where there is a 'one-to-one' relationship and the table it applies to does not have the foreign key. has_one
声明仅在存在“一对一”关系且其应用的表没有外键的情况下使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.