I need to need to make a column as foreign key. I did lot of research on it. I understood that I need to add associations. I am clear on belongs_to , has_one and has_many options. After creating associations rails know that there is a foreign key association.If I remove the main record then dependent record will be deleted by rails app. I was reading on migrations and I came across http://edgeguides.rubyonrails.org/active_record_migrations.html where it is mentioned: $ bin/rails generate migration AddUserRefToProducts user:references
will generate:
class AddUserRefToProducts < ActiveRecord::Migration
def change
add_reference :products, :user, index: true, foreign_key: true
end
end
Now on website: http://guides.rubyonrails.org/active_record_migrations.html where it is mentioned: $ bin/rails generate migration AddUserRefToProducts user:references
will generate:
class AddUserRefToProducts < ActiveRecord::Migration
def change
add_reference :products, :user, index: true
end
end
I understand the creation of index. Do I need to have foreign_key: true explicitly or not? what difference will it make?