生成迁移-创建联接表 [英] Generate migration - create join table
问题描述
我浏览了许多SO
和google
帖子,以生成用于has many and belongs to many
关联的联接表迁移,并且没有任何作用.
I have looked through many SO
and google
posts for generating migration of join table for has many and belongs to many
association and nothing work.
所有解决方案都在生成一个空的迁移文件.
All of the solutions are generating a empty migration file.
我正在使用rails 3.2.13
,我有两个表:security_users
和assignments
.这些是我尝试过的一些事情:
I am using rails 3.2.13
and I have two tables: security_users
and assignments
. These are some of things I have try:
rails generate migration assignments_security_users
rails generate migration create_assignments_security_users
rails generate migration create_assignments_security_users_join_table
rails g migration create_join_table :products, :categories (following the official documentation)
rails generate migration security_users_assignments security_user:belongs_to assignments:belongs_to
谁能说出如何在两个表之间创建联接表迁移?
Can anyone tell how to create a join table migration between two tables?
推荐答案
运行以下命令以生成空的迁移文件(该文件不会自动填充,您需要自己填充):
Run this command to generate the empty migration file (it is not automatically populated, you need to populate it yourself):
rails generate migration assignments_security_users
打开生成的迁移文件并添加以下代码:
Open up the generated migration file and add this code:
class AssignmentsSecurityUsers < ActiveRecord::Migration
def change
create_table :assignments_security_users, :id => false do |t|
t.integer :assignment_id
t.integer :security_user_id
end
end
end
然后从终端运行rake db:migrate
.我通过一个简单的示例创建了关于many_to_many关系的测验可能会对您有帮助.
Then run rake db:migrate
from your terminal. I created a quiz on many_to_many relationships with a simple example that might help you.
这篇关于生成迁移-创建联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!