HXH
HXH

Reputation: 1663

database index in Active Record Associations

let's imagine this situation.I have a article model and a sort model,and they have a one-to-many association.

the article migration is :

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

  t.timestamps
end

the sort migration is :

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

    t.timestamps
end

so the article will be added a filed named sort_id.

now,I want to add an index on the filed sort_id in table articles.

whether I can realize as follow:

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

or

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

Upvotes: 0

Views: 27

Answers (1)

Krishna Rani Sahoo
Krishna Rani Sahoo

Reputation: 1539

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

Upvotes: 1

Related Questions