【发布时间】:2019-03-17 04:25:42
【问题描述】:
我在阅读 Rails 指南时发现了一种用于创建连接表的新语法:
class CreateJoinTableCustomerProduct < ActiveRecord::Migration[5.0]
def change
create_join_table :customers, :products do |t|
# t.index [:customer_id, :product_id]
# t.index [:product_id, :customer_id]
end
end
end
也许这个辅助方法 create_join_table 是 Rails 5 中的新方法,它在 schema.rb 中生成适当的连接表。但正是这部分让我感到不安:
# t.index [:customer_id, :product_id]
# t.index [:product_id, :customer_id]
通常,如果您想为列添加索引,您可以执行以下操作:
add_index :products, :product_id
但是为什么在这个迁移中有两个索引,具有相同的两列?这个语法的解释是什么?
【问题讨论】:
标签: ruby-on-rails