Ruby-on-rails Rails中has_one和belongs_to的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/861144/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:14:42  来源:igfitidea点击:

Difference between has_one and belongs_to in Rails?

ruby-on-railsrubyhas-one

提问by Moon

I am trying to understand has_onerelationship in RoR.

我试图了解has_oneRoR 中的关系。

Let's say I have two models - Personand Cell:

假设我有两个模型 -PersonCell

class Person < ActiveRecord::Base
  has_one :cell
end

class Cell < ActiveRecord::Base
  belongs_to :person
end

Can I just use has_one :personinstead of belongs_to :personin Cellmodel?

我可以只使用has_one :person而不是belongs_to :personCell模型中吗?

Isn't it the same?

不是一样吗?

回答by Sarah Mei

No, they are not interchangable, and there are some real differences.

不,它们不可互换,并且存在一些真正的差异。

belongs_tomeans that the foreign key is in the table for this class. So belongs_tocan ONLY go in the class that holds the foreign key.

belongs_to意味着外键在这个类的表中。所以belongs_to只能进入持有外键的类。

has_onemeans that there is a foreign key in another table that references this class. So has_onecan ONLY go in a class that is referenced by a column in another table.

has_one意味着在另一个表中有一个引用这个类的外键。所以has_one只能进入由另一个表中的列引用的类。

So this is wrong:

所以这是错误的:

class Person < ActiveRecord::Base
  has_one :cell # the cell table has a person_id
end

class Cell < ActiveRecord::Base
  has_one :person # the person table has a cell_id
end

And this is also wrong:

这也是错误的:

class Person < ActiveRecord::Base
  belongs_to :cell # the person table has a cell_id
end

class Cell < ActiveRecord::Base
  belongs_to :person # the cell table has a person_id
end

The correct way is (if Cellcontains person_idfield):

正确的方法是(如果Cell包含person_id字段):

class Person < ActiveRecord::Base
  has_one :cell # the person table does not have 'joining' info
end

class Cell < ActiveRecord::Base
  belongs_to :person # the cell table has a person_id
end

For a two-way association, you need one of each, and they have to go in the right class. Even for a one-way association, it matters which one you use.

对于双向关联,您需要每个关联一个,并且他们必须进入正确的班级。即使对于单向关联,使用哪一个也很重要。

回答by Pablo Fernandez

If you add "belongs_to" then you got a bidirectional association. That means you can get a person from the cell and a cell from the person.

如果您添加“belongs_to”,那么您将获得双向关联。这意味着你可以从细胞中得到一个人,也可以从这个人那里得到一个细胞。

There's no real difference, both approaches (with and without "belongs_to") use the same database schema (a person_id field in the cells database table).

没有真正的区别,两种方法(有和没有“belongs_to”)都使用相同的数据库模式(单元数据库表中的 person_id 字段)。

To summarize: Do not add "belongs_to" unless you need bidirectional associations between models.

总结一下:除非您需要模型之间的双向关联,否则不要添加“belongs_to”。

回答by Jarrod

Using both allows you to get info from both Person and Cell models.

使用两者可以让您从 Person 和 Cell 模型中获取信息。

@cell.person.whatever_info and @person.cell.whatever_info.