Ruby-on-rails own_to 和 has_one 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3808926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between belongs_to and has_one?
提问by Blankman
What is the difference between a belongs_toand a has_one?
abelongs_to和 a 和有has_one什么不一样?
Reading the Ruby on Rails guide hasn't helped me.
阅读 Ruby on Rails 指南并没有帮助我。
回答by ryeguy
They essentially do the same thing, the only difference is what side of the relationship you are on. If a Userhas a Profile, then in the Userclass you'd have has_one :profileand in the Profileclass you'd have belongs_to :user. To determine who "has" the other object, look at where the foreign key is. We can say that a User"has" a Profilebecause the profilestable has a user_idcolumn. If there was a column called profile_idon the userstable, however, we would say that a Profilehas a User, and the belongs_to/has_one locations would be swapped.
他们基本上做同样的事情,唯一的区别是你在关系的哪一边。如果 aUser有 a Profile,那么在User您的班级中您将拥有,has_one :profile而在Profile您的班级中您将拥有belongs_to :user。要确定谁“拥有”另一个对象,请查看外键的位置。我们可以说 a User“有” aProfile因为该profiles表有一user_id列。然而,如果表profile_id上有一列被调用users,我们会说 aProfile有 a User,并且belongs_to/has_one 位置将被交换。
hereis a more detailed explanation.
这里有更详细的解释。
回答by Chandan Kumar Mallik
It's about where the foreign key sits.
这是关于外键所在的位置。
class Foo < AR:Base
end
- If foo
belongs_to :bar, then the foos table has abar_idcolumn - If foo
has_one :bar, then the bars table has afoo_idcolumn
- 如果 foo
belongs_to :bar,则 foos 表有一bar_id列 - 如果 foo
has_one :bar,则 bar 表有一foo_id列
On the conceptual level, if your class Ahas a has_onerelationship with class Bthen class Ais the parent of class Bhence your class Bwill have a belongs_torelationship with class Asince it is the child of class A.
在概念层面上,如果您class A与 有has_one关系,class B则class A是 的父级,class B因此您class B将与 有belongs_to关系,class A因为它是 的子级class A。
Both express a 1-1 relationship. The difference is mostly where to place the foreign key, which goes on the table for the class declaring the belongs_torelationship.
两者都表达了 1-1 的关系。区别主要在于外键的放置位置,外键位于声明belongs_to关系的类的表中。
class User < ActiveRecord::Base
# I reference an account.
belongs_to :account
end
class Account < ActiveRecord::Base
# One user references me.
has_one :user
end
The tables for these classes could look something like:
这些类的表可能类似于:
CREATE TABLE users (
id int(11) NOT NULL auto_increment,
account_id int(11) default NULL,
name varchar default NULL,
PRIMARY KEY (id)
)
CREATE TABLE accounts (
id int(11) NOT NULL auto_increment,
name varchar default NULL,
PRIMARY KEY (id)
)
回答by illusionist
has_oneand belongs_togenerally are same in a sense that they point to the other related model. belongs_tomake sure that this model has the foreign_keydefined.
has_onemakes sure that the other model has_foreignkey defined.
has_one并且belongs_to通常在某种意义上是相同的,它们指向另一个相关模型。belongs_to确保该模型具有foreign_key定义。
has_one确保has_foreign定义了另一个模型键。
To be more specific, there are two sides of relationship, one is the Ownerand other is Belongings. If only has_oneis defined we can get its Belongingsbut cannot get the Ownerfrom the belongings. To trace the Ownerwe need to define the belongs_toas well in the belonging model.
更具体地说, 有两个方面relationship,一个是Owner,另一个是Belongings。如果只has_one定义了,我们可以得到它,Belongings但不能Owner从belongings. 为了追踪Owner我们需要belongs_to在归属模型中定义。
回答by Somesh Sharma
One additional thing that i want to add is, Suppose we have following models association
我想补充的另一件事是,假设我们有以下模型关联
class Author < ApplicationRecord
has_many :books
end
class Author < ApplicationRecord
has_many :books
end
if we only write the above association then we can get all books of a particular author by,
如果我们只编写上述关联,那么我们可以通过以下方式获取特定作者的所有书籍,
@books = @author.books
But for a particular book we can't get the corresponding author by,
但是对于特定的书,我们无法通过,
@author = @book.author
to make the above code work work we need to add association to Book model also, like this
为了使上面的代码工作,我们还需要向 Book 模型添加关联,就像这样
class Book < ApplicationRecord
belongs_to :author
end
This will add method 'author' to Book model.
For mode details see guides
这将向 Book 模型添加方法“作者”。
有关模式详细信息,请参阅指南
回答by konyak
From a simplicity standpoint, belongs_tois better than has_onebecause in has_one, you would have to add the following constraints to the model and table that has the foreign key to enforce the has_onerelationship:
从简单的角度来看,belongs_to比has_onein 更好has_one,您必须向具有外键的模型和表添加以下约束以强制执行has_one关系:
validates :foreign_key, presence: true, uniqueness: true- add a database unique index on the foreign key.
validates :foreign_key, presence: true, uniqueness: true- 在外键上添加数据库唯一索引。

