生成迁移 - 创建连接表 [英] Generate migration - create join table

查看:25
本文介绍了生成迁移 - 创建连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了许多 SOgoogle 帖子,用于生成 有很多并且属于很多 关联的连接表的迁移,但没有任何效果.

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_usersassignments.这些是我尝试过的一些事情:

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.我用一个简单的例子创建了一个关于多对多关系的测验这可能对你有帮助.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆