Rails has_and_belongs_to_many 迁移 [英] Rails has_and_belongs_to_many migration

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

问题描述

我有两个模型 restaurantuser 我想要执行 has_and_belongs_to_many 关系.

I have two models restaurant and user that I want to perform a has_and_belongs_to_many relationship.

我已经进入模型文件并添加了 has_and_belongs_to_many :restaurantshas_and_belongs_to_many :users

I have already gone into the model files and added the has_and_belongs_to_many :restaurants and has_and_belongs_to_many :users

我认为此时我应该可以用 Rails 3 做一些事情:

I assume at this point I should be able to do something like with Rails 3:

rails generate migration ....

但我尝试过的一切似乎都失败了.我确定这是一个非常简单的东西,我是 Rails 的新手,所以我仍在学习.

but everything I have tried seems to fail. I'm sure this is something really simple I'm new to rails so I'm still learning.

推荐答案

需要单独添加一个只有restaurant_iduser_id(无主键)的join表, 按字母顺序.

You need to add a separate join table with only a restaurant_id and user_id (no primary key), in alphabetical order.

首先运行您的迁移,然后编辑生成的迁移文件.

First run your migrations, then edit the generated migration file.

Rails 3

rails g migration create_restaurants_users_table

Rails 4:

rails g migration create_restaurants_users

Rails 5

rails g migration CreateJoinTableRestaurantUser restaurants users

来自文档:

还有一个生成器,如果 JoinTable是名称的一部分:

There is also a generator which will produce join tables if JoinTable is part of the name:

<小时>

您的迁移文件(注意 :id => false;这是阻止创建主键的原因):


Your migration file (note the :id => false; it's what prevents the creation of a primary key):

Rails 3

class CreateRestaurantsUsers < ActiveRecord::Migration
  def self.up
    create_table :restaurants_users, :id => false do |t|
        t.references :restaurant
        t.references :user
    end
    add_index :restaurants_users, [:restaurant_id, :user_id]
    add_index :restaurants_users, :user_id
  end

  def self.down
    drop_table :restaurants_users
  end
end

Rails 4

class CreateRestaurantsUsers < ActiveRecord::Migration
  def change
    create_table :restaurants_users, id: false do |t|
      t.belongs_to :restaurant
      t.belongs_to :user
    end
  end
end

t.belongs_to 将自动创建必要的索引.def change 将自动检测向前或回滚迁移,无需向上/向下.

t.belongs_to will automatically create the necessary indices. def change will auto detect a forward or rollback migration, no need for up/down.

Rails 5

create_join_table :restaurants, :users do |t|
  t.index [:restaurant_id, :user_id]
end

注意:还有一个自定义表名选项,可以作为参数传递给名为 table_name 的 create_join_table.来自 文档

Note: There is also an option for a custom table name that can be passed as a parameter to create_join_table called table_name. From the docs

默认情况下,连接表的名称来自于按字母顺序提供给 create_join_table 的前两个参数命令.要自定义表的名称,请提供 :table_name选项:

By default, the name of the join table comes from the union of the first two arguments provided to create_join_table, in alphabetical order. To customize the name of the table, provide a :table_name option:

这篇关于Rails has_and_belongs_to_many 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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