ma1*_*w28 122 migration code-generation ruby-on-rails has-and-belongs-to-many
如何script/generate migration为has_and_belongs_to_many关系创建连接表?
该应用程序在Rails 2.3.2上运行,但我也安装了Rails 3.0.3.
dan*_*ave 227
哪里:
class Teacher < ActiveRecord::Base
  has_and_belongs_to_many :students
end
和
class Student < ActiveRecord::Base
  has_and_belongs_to_many :teachers
end
对于铁轨4:
rails generate migration CreateJoinTableStudentTeacher student teacher
对于rails 3:
rails generate migration students_teachers student_id:integer teacher_id:integer
for rails <3
script/generate migration students_teachers student_id:integer teacher_id:integer
(注意表名按字母顺序列出两个连接表)
然后仅对rails 3及以下版本,您需要编辑生成的迁移,以便不创建id字段:
create_table :students_teachers, :id => false do |t|
doc*_*hat 138
一个has_and_belongs_to_many表必须格式相匹配.我假设要加入的两个模型has_and_belongs_to_many已经在DB中了:apples和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. Don't use the
# unique if you allow duplicates.
add_index(:apples_oranges, [:apple_id, :orange_id], :unique => true)
如果你使用:unique => true索引,那么你应该(在rails3中)传递:uniq => true给has_and_belongs_to_many.
更多信息:Rails Docs
已更新2010-12-13我已经更新了它删除ID和时间戳...基本上MattDiPasquale和nunopolonia是正确的:不能有一个ID,并不能有时间戳或导轨不允许has_and_belongs_to_many工作.
nun*_*nia 14
你应该命名表要通过字母顺序连接的2种型号的名称,并将这两个模型的ID在表中.然后将每个模型相互连接,在模型中创建关联.
这是一个例子:
# 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
但这不是很灵活,你应该考虑使用has_many:through
最上面的答案显示了一个综合索引,我不相信它将用于从橙子中查找苹果.
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)
我确实找到了答案,这是基于'医生什么'有用,讨论当然也是如此.
Associations Rails Guide的 HABTM 部分很棒,但没有准确解释如何创建连接表。
然而,Migrations Rails Guide解释了如何创建连接表:
迁移方法
create_join_table创建一个 HABTM(具有且属于多个)连接表。典型的用途是:
create_join_table :products, :categories默认情况下,连接表的名称来自提供给 create_join_table 的前两个参数的并集,按字母顺序排列。