Active Record Associations中的数据库索引

时间:2013-09-26 09:40:19

标签: ruby-on-rails migration

让我们想象一下这种情况。我有一个article模型和一个sort模型,他们有一对多关联。

article迁移是:

create_table :articles do |t|
  t.string :title      
  t.string :content
  t.belongs_to :sort

  t.timestamps
end

sort迁移是:

create_table :sorts do |t|
    t.string :title  

    t.timestamps
end

因此,article将添加名为sort_id的文件。

现在,我想在表sort_id中的字段articles上添加索引。

我是否可以实现如下:

   add_index :articles, :sort, :unique => true 

  add_index :articles, :sort_id, :unique => true 

1 个答案:

答案 0 :(得分:1)

add_index :articles, :sort_id, :unique => true