Ruby-on-rails has_and_belongs_to_many 连接表的 Rails 迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4381154/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:49:54  来源:igfitidea点击:

Rails migration for has_and_belongs_to_many join table

ruby-on-railsmigrationcode-generationhas-and-belongs-to-many

提问by ma11hew28

How do I do a script/generate migrationto create a join table for a has_and_belongs_to_manyrelationship?

如何script/generate migrationhas_and_belongs_to_many关系创建连接表?

The application runs on Rails 2.3.2, but I also have Rails 3.0.3 installed.

该应用程序在 Rails 2.3.2 上运行,但我也安装了 Rails 3.0.3。

回答by dangerousdave

Where:

在哪里:

class Teacher < ActiveRecord::Base
  has_and_belongs_to_many :students
end

and

class Student < ActiveRecord::Base
  has_and_belongs_to_many :teachers
end


for rails 4:

对于导轨 4:

rails generate migration CreateJoinTableStudentTeacher student teacher


for rails 3:

对于导轨 3:

rails generate migration students_teachers student_id:integer teacher_id:integer


for rails < 3

对于导轨 < 3

script/generate migration students_teachers student_id:integer teacher_id:integer

(note the table name lists both join tables in alphabetical order)

(注意表名按字母顺序列出了两个连接表)

and then for rails 3 and below only, you need to edit your generated migration so an id field is not created:

然后仅对于 rails 3 及以下版本,您需要编辑生成的迁移,以便不会创建 id 字段:

create_table :students_teachers, :id => false do |t|

回答by docwhat

A has_and_belongs_to_manytable must match this format. I'm assuming the two models to be joined by has_and_belongs_to_manyare already in the DB : applesand oranges:

一个has_and_belongs_to_many表必须格式相匹配。我假设要加入的两个模型has_and_belongs_to_many已经在数据库中:applesoranges

create_table :apples_oranges, :id => false do |t|
  t.references :apple, :null => false
  t.references :orange, :null => false
end

# Adding the index can massively speed up join tables. Don't use the
# unique if you allow duplicates.
add_index(:apples_oranges, [:apple_id, :orange_id], :unique => true)

If you use the :unique => trueon the index, then you should (in rails3) pass :uniq => trueto has_and_belongs_to_many.

如果您:unique => true在索引上使用,那么您应该(在 rails3 中)传递:uniq => truehas_and_belongs_to_many.

More information: Rails Docs

更多信息:Rails 文档

UPDATED 2010-12-13I've updated it to remove the id and timestamps... Basically MattDiPasqualeand nunopoloniaare correct: There must not be an id and there must not be timestamps or rails won't allow has_and_belongs_to_manyto work.

2010 年 12 月 13 日更新我已经更新它以删除 id 和时间戳......基本上MattDiPasquale并且nunopolonia是正确的:不能有 id 并且不能有时间戳或 rails 将不允许has_and_belongs_to_many工作。

回答by nunopolonia

You should name the table the names of 2 models you want to connect by alphabetical order and put the two model id's in the table. Then connect each model to each other creating the associations in the model.

您应该将表命名为要按字母顺序连接的 2 个模型的名称,并将两个模型 ID 放入表中。然后将每个模型相互连接,在模型中创建关联。

Here's an example:

下面是一个例子:

# in migration
def self.up
  create_table 'categories_products', :id => false do |t|
    t.column :category_id, :integer
    t.column :product_id, :integer
  end
end

# models/product.rb
has_and_belongs_to_many :categories

# models/category.rb
has_and_belongs_to_many :products

But this is not very flexible and you should think about using has_many :through

但这不是很灵活,您应该考虑使用 has_many :through

回答by Joseph Lord

The top answer shows a composite index that I don't believe will be used to lookup apples from oranges.

最上面的答案显示了一个复合索引,我认为它不会用于从橙子中查找苹果。

create_table :apples_oranges, :id => false do |t|
  t.references :apple, :null => false
  t.references :orange, :null => false
end

# Adding the index can massively speed up join tables.
# This enforces uniqueness and speeds up apple->oranges lookups.
add_index(:apples_oranges, [:apple_id, :orange_id], :unique => true)
# This speeds up orange->apple lookups
add_index(:apples_oranges, :orange_id)

I did find the answer this is based on by 'The Doctor What' useful and the discussion certainly so too.

我确实发现这是基于“The Doctor What”的有用答案,讨论当然也是如此。

回答by zw963

In rails 4, you can simple use

在rails 4中,你可以简单的使用

create_join_table :table1s, :table2s

create_join_table :table1s, :table2s

it is all.

这就是全部。

Caution: you must offord table1, table2 with alphanumeric.

注意:您必须不使用带有字母数字的表 1、表 2。

回答by Jwan622

I like doing:

我喜欢做:

rails g migration CreateJoinedTable model1:references model2:references. That way I get a migration that looks like this:

rails g migration CreateJoinedTable model1:references model2:references. 这样我就得到了一个看起来像这样的迁移:

class CreateJoinedTable < ActiveRecord::Migration
  def change
    create_table :joined_tables do |t|
      t.references :trip, index: true
      t.references :category, index: true
    end
    add_foreign_key :joined_tables, :trips
    add_foreign_key :joined_tables, :categories
  end
end

I like having index on these columns because I'll often be doing lookups using these columns.

我喜欢在这些列上建立索引,因为我经常使用这些列进行查找。