2016-11-24 105 views
0

我有以下型号:belongs_to在Rails 5中创建关联吗?

class User 
    has_many :items 
end 

class Item 
    belongs_to :user 
end 

create_tableitems我:

t.belongs_to :person 

我用Rails 5,我看到这个自动创建person_iditems表中的索引。但是,根据这个SO thread,这不应该发生。

答案是不正确的,或者这是添加到Rails 5(或某些Rails 4.x版本)?

+0

'person_id'在迁移和类是用户。这是正确的吗?因为如果是这样,这改变了事情。 – lcguida

回答

2

是否belongs_to在Rails 5中创建关联?

是的。

belongs_toreferences一个别名,它:

  • 创建在itemsperson_id柱;
  • person_id列上加上索引。
+0

但它不在Rails 4中,对吧? – anemaria20

+0

@ anemaria20我不记得,也不能检查我离开笔记本电脑。你可以检查相应的轨道4文档或测试它,因为我会做:) –

0

它不应该。

As @Andrey提到,belongs_toreferences的别名。如果你按照文档,你会发现references内部使用connection.add_reference。在connection.add_reference文档,你可以看到options,及旺角他们index

:index 

    Add an appropriate index. Defaults to false. 

    See add_index for usage of this option. 

所以,默认情况下,index选项,这增加了索引数据库设置为false。您应该明确地将其设置在references中。

相关问题