2014-01-10 39 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