I have 2 models:
Account
Profile
creating these models created 2 tables in the database:
accounts
profiles
now I want to add a relationship:
- each account can have many profiles
- each profile belongs to one account
I ran the following command:
rails g migration AddAccountToProfiles account:references
which created the following migration:
class AddAccountToProfiles < ActiveRecord::Migration
def change
add_reference :profiles, :account, index: true, foreign_key: true
end
end
now I'm a little confused:
why does the migration say :profiles
and :account
? Shouldn't it be :accounts
(plural)?
also, after (or before) creating this migration, I have to add belongs_to
and has_many
in the appropriate model class right?
as a side question, is there a way to add belongs_to
and has_many
in the models, and from that information have rails generate the appropriate migration without me manually creating a migration with the 'rails g migration ...'
command?