Ruby-on-rails 使用 user:references 和 user_id:integer 生成模型

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

generate model using user:references vs user_id:integer

ruby-on-rails

提问by stackOverlord

I'm confused on how to generate a model that belongs_to another model. My book uses this syntax to associate Micropost with User:

我对如何生成一个属于另一个模型的模型感到困惑。我的书使用这种语法将 Micropost 与用户关联起来:

rails generate model Micropost user_id:integer

but http://guides.rubyonrails.org/says to do it like this:

但是http://guides.rubyonrails.org/说要这样做:

rails generate model Micropost user:references

The migrations generated by these 2 are different. Also, for the former, how does rails know that user_idis a foreign key referencing user? Thanks!

这两个生成的迁移是不同的。另外,对于前者,rails 如何知道这user_id是外键引用user?谢谢!

回答by Jon M.

Both will generate the same columns when you run the migration. In rails console, you can see that this is the case:

运行迁移时,两者都会生成相同的列。在 rails 控制台中,可以看到是这样的:

:001 > Micropost
=> Micropost(id: integer, user_id: integer, created_at: datetime, updated_at: datetime)

The second command adds a belongs_to :userrelationship in your Micropost model whereas the first does not. When this relationship is specified, ActiveRecord will assume that the foreign key is kept in the user_idcolumn and it will use a model named Userto instantiate the specific user.

第二个命令belongs_to :user在您的 Micropost 模型中添加了一个关系,而第一个则没有。指定此关系后,ActiveRecord 将假定外键保留在user_id列中,并将使用命名的模型User来实例化特定用户。

The second command also adds an index on the new user_idcolumn.

第二个命令还在新user_id列上添加索引。

回答by illusionist

how does rails know that user_idis a foreign key referencing user?

rails 如何知道这user_id是外键引用user

Rails itself does not know that user_idis a foreign key referencing user. In the first command rails generate model Micropost user_id:integerit only adds a column user_idhowever rails does not know the use of the col. You need to manually put the line in the Micropostmodel

Rails 本身并不知道这user_id是一个外键引用user。在第一个命令中rails generate model Micropost user_id:integer,它只添加了一个列,user_id但是 rails 不知道 col 的使用。您需要手动将线放入Micropost模型中

class Micropost < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :microposts
end

the keywords belongs_toand has_manydetermine the relationship between these models and declare user_idas a foreign key to Usermodel.

关键字belongs_tohas_many确定这些模型之间的关系并声明user_idUser模型的外键。

The later command rails generate model Micropost user:referencesadds the line belongs_to :userin the Micropostmodel and hereby declares as a foreign key.

后面的命令在模型中rails generate model Micropost user:references添加了该行belongs_to :userMicropost并在此声明为外键。

FYI
Declaring the foreign keys using the former method only lets the Rails know about the relationship the models/tables have. The database is unknown about the relationship. Therefore when you generate the EER Diagrams using software like MySql Workbenchyou find that there is no relationship threads drawn between the models. Like in the following pic enter image description here

仅供参考
使用前一种方法声明外键只会让 Rails 了解模型/表之间的关系。数据库对这种关系是未知的。因此,当您使用类似软件生成 EER 图表时,您MySql Workbench会发现模型之间没有绘制关系线程。如下图所示 在此处输入图片说明

However, if you use the later method you find that you migration file looks like:

但是,如果您使用后一种方法,您会发现您的迁移文件如下所示:

def change
    create_table :microposts do |t|
      t.references :user, index: true

      t.timestamps null: false
    end
    add_foreign_key :microposts, :users

Now the foreign key is set at the database level. and you can generate proper EERdiagrams. enter image description here

现在外键是在数据库级别设置的。你可以生成适当的EER图表。 在此处输入图片说明

回答by Krule

For the former, convention over configuration. Rails default when you reference another table with

对于前者,约定优于配置。引用另一个表时,Rails 默认

 belongs_to :something

is to look for something_id.

是寻找something_id

references, or belongs_tois actually newer way of writing the former with few quirks.

references,或者belongs_to实际上是写前者的新方式,几乎没有怪癖。

Important is to remember that it will not create foreign keys for you. In order to do that, you need to set it up explicitly using either:

重要的是要记住它不会为您创建外键。为此,您需要使用以下任一方法显式设置它:

t.references :something, foreign_key: true
t.belongs_to :something_else, foreign_key: true

or (note the plural):

或(注意复数):

add_foreign_key :table_name, :somethings
add_foreign_key :table_name, :something_elses`