让我们想象一下这种情况。我有一个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
答案 0 :(得分:1)
add_index :articles, :sort_id, :unique => true