Ruby-on-rails 将关联添加到现有模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15385087/
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
add associations to exisiting models
提问by Andrew
I'm wondering how I can add associations to my models. Suppose, I generate two models
我想知道如何为我的模型添加关联。假设,我生成了两个模型
rails generate model User
rails generate model Car
Now I want to add an associations so that the models acquire the form
现在我想添加一个关联,以便模型获取表单
class User < ActiveRecord::Base
has_many :cars
end
class Car < ActiveRecord::Base
belongs_to :user
end
The question is: how to apply this modification by migrations in order to obtain cars_users table in the database? I'm planning to use that table in my code.
问题是:如何通过迁移应用此修改以获取数据库中的cars_users 表?我打算在我的代码中使用该表。
回答by jvnill
belongs_toassociation expect an association_idcolumn in its corresponding table. Since cars belongs_to user, the cars table should have a user_idcolumn. This can be accomplished 2 ways.
belongs_to关联期望association_id其对应表中的列。由于汽车属于用户,汽车表应该有一user_id列。这可以通过两种方式完成。
first, you can generate the column when you create the model
首先,您可以在创建模型时生成列
rails g model car user_id:references
or just add the user_id after you create the model like Richard Brown's answer. Be careful that if you use integerinstead of references, you'd have to create the index yourself.
或者在创建模型后添加 user_id 就像 Richard Brown 的回答一样。请注意,如果使用integer而不是references,则必须自己创建索引。
rails g migration add_user_id_to_cars user_id:integer
then in the generated migration, add
然后在生成的迁移中,添加
add_index :cars, :user_id
UPDATE:
更新:
As Joseph has mentioned in the comments, the need to add the index manually has already been addressed in the current version of Rails. I think it was introduced in Rails 4. You can read more of it in the official Rails guide for migrations. The gist of it is running the following generator
正如 Joseph 在评论中提到的那样,当前版本的 Rails 已经解决了手动添加索引的需要。我认为它是在 Rails 4 中引入的。您可以在官方 Rails 迁移指南中阅读更多内容。它的要点是运行以下生成器
bin/rails g migration add_user_to_cars user:references
will create a migration with a line similar to
将创建一个类似于行的迁移
add_reference :cars, :user, index: true
This will add a user_idcolumn to the cars table and it will also mark that column to be indexed.
这将user_id在cars 表中添加一列,并且还会将该列标记为要建立索引。
回答by SimonW
Following @jvnill's explanation in rails 4(and maybe in rails 3.2 too) you can do it like this too (avoiding the id parts and remembering the exact convetions):
按照@jvnill 在rails 4 中的解释(也可能在 rails 3.2 中),您也可以这样做(避免 id 部分并记住确切的对流):
rails g migration AddUserToCar user:references
Which will create the following migration, taking care of both adding the column and index with all correct conventions:
这将创建以下迁移,注意添加具有所有正确约定的列和索引:
class AddUserToCar < ActiveRecord::Migration
def change
add_reference :cars, :user, index: true
end
end
At the end as always run the migration:
最后一如既往地运行迁移:
rake db:migrate
View your schema.rbto view the new index and user_id column.
查看您schema.rb以查看新索引和 user_id 列。
回答by Richard Brown
Generate a migration to create the association:
生成迁移以创建关联:
rails g migration AddUserIdToCars user_id:integer
rake db:migrate
回答by vinoth
Migration file:
迁移文件:
class Createuser < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :name
end
create_table :cars do |t|
t.belongs_to :user, index: true
t.varchar(255) :model
t.varchar(255) :color
end
end
end

