Rails has_and_belongs_to_many 迁移

发布于 2024-11-18 00:23:56 字数 356 浏览 2 评论 0原文

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

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

我认为此时我应该能够执行类似于 Rails 3 的操作:

rails generate migration ....

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

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

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

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

rails generate migration ....

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江南烟雨〆相思醉 2024-11-25 00:23:56

您需要添加一个单独的联接表,其中仅包含 restaurant_iduser_id(无主键),并按字母顺序排列。

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

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 则将生成连接表
是名称的一部分:


您的迁移文件(请注意 :id => false;它会阻止创建主键):

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 将自动创建必要的索引。 defchange 将自动检测前向或回滚迁移,无需向上/向下。

Rails 5

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

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

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

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

From the docs:

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


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

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

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:

惯饮孤独 2024-11-25 00:23:56

这里的答案已经相当过时了。从 Rails 4.0.2 开始,您的迁移使用 create_join_table

要创建迁移,请运行:

rails g migration CreateJoinTableRestaurantsUsers restaurant user

这将生成以下内容:

class CreateJoinTableRestaurantsUsers < ActiveRecord::Migration
  def change
    create_join_table :restaurants, :users do |t|
      # t.index [:restaurant_id, :user_id]
      # t.index [:user_id, :restaurant_id]
    end
  end
end

如果您想对这些列建立索引,请取消注释相应的行,然后就可以开始了!

The answers here are quite dated. As of Rails 4.0.2, your migrations make use of create_join_table.

To create the migration, run:

rails g migration CreateJoinTableRestaurantsUsers restaurant user

This will generate the following:

class CreateJoinTableRestaurantsUsers < ActiveRecord::Migration
  def change
    create_join_table :restaurants, :users do |t|
      # t.index [:restaurant_id, :user_id]
      # t.index [:user_id, :restaurant_id]
    end
  end
end

If you want to index these columns, uncomment the respective lines and you're good to go!

乖乖 2024-11-25 00:23:56

创建联接表时,请特别注意迁移名称/类中两个表需要按字母顺序列出的要求。如果您的型号名称相似,例如“abc”和“abb”,这很容易让您感到困扰。如果你要竞选,

rails g migration create_abc_abb_table

你的关系将无法按预期运作。您必须使用

rails g migration create_abb_abc_table

它来代替。

When creating the join table, pay careful attention to the requirement that the two tables need to be listed in alphabetical order in the migration name/class. This can easily bite you if your model names are similar, e.g. "abc" and "abb". If you were to run

rails g migration create_abc_abb_table

Your relations will not work as expected. You must use

rails g migration create_abb_abc_table

instead.

水染的天色ゝ 2024-11-25 00:23:56

对于 HABTM 关系,您需要创建一个联接表。只有连接表,并且该表不应有 id 列。尝试这个迁移。

def self.up
  create_table :restaurants_users, :id => false do |t|
    t.integer :restaurant_id
    t.integer :user_id
  end
end

def self.down
  drop_table :restaurants_users
end

您必须检查 这种关系rails指导教程

For HABTM relationships, you need to create a join table. There is only join table and that table should not have an id column. Try this migration.

def self.up
  create_table :restaurants_users, :id => false do |t|
    t.integer :restaurant_id
    t.integer :user_id
  end
end

def self.down
  drop_table :restaurants_users
end

You must check this relationship rails guide tutorials

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文