ruby on rails - Does has_many realation require foreign key? -
ruby on rails - Does has_many realation require foreign key? -
i wan create simple has_many
, belongs_to
relation between models. have user
model , quote
model. , each quote belongs user , evry user has many quotes.
i've studied this: http://guides.rubyonrails.org/association_basics.html
but still don't know foreign key. rails automatically in background (when add together has_many
, belongs_to
) or should add together manually migration user_id
column added quotes
table? should have user_id
column in db_schema
?
the foreign keys should defined in migration. if utilize generator, rails generate migration, maybe looks following:
class createquotes < activerecord::migration def alter create_table :quotes |t| t.string :title, null: false t.text :content, null: false t.references :user t.timestamps end end end
the statement t.references :user
generate foreign key column, called user_id
in case.
here quote rails guides:
using t.integer :supplier_id makes foreign key naming obvious , explicit. in current versions of rails, can abstract away implementation detail using t.references :supplier instead.
ruby-on-rails ruby-on-rails-4
Comments
Post a Comment