2014-01-10 33 views
1
rails g migration CreateJoinTable zombie:index role:index 

这就造成这种迁移:有效地创建一个连接表迁移

class CreateJoinTable < ActiveRecord::Migration 
    def change 
    create_join_table :zombies, :roles do |t| 
     t.index [:zombie_id, :role_id] 
     t.index [:role_id, :zombie_id] # I'd be happy if it didn't have this! 
    end 
    end 
end 

,移民几乎是有,但为什么我有四个指标,而不是两个?在我的generate命令中,它是否指定为已经存在的索引创建额外的两组索引?

回答

3

试试这个:

rails g migration CreateJoinTableRolesZombies roles zombies 

迁移注释掉指标,大概是为了表明create_join_table为您处理此。

请注意,在导轨4中,表名必须按排序顺序排列。此外,为了清楚起见,在此示例中扩展了迁移名称。 CreateJoinTable出现在它中,这就足够了。

+0

供参考:http://edgeguides.rubyonrails.org/active_record_migrations.html#creating-a-join-table 也是这样: http://edgeguides.rubyonrails.org/active_record_migrations.html#creating-独立迁移(本部分的最后部分) – sixty4bit