views:

12

answers:

1

I execute the following commands to make the model:

script/generate model user firstname:string lastname:string

script/generate song group songname:string songtitle:string

a user has_many :songs and a song belongs_to :user

after this I run rake db:migrate however, the associations are not carried to my actual DB. Because in my actual DB I do not see any user_id column in songs table...etc.

Do we have to manually change the migration and add the needed columns?

A: 

To the best of my knowledge, you need to explicitly add the user_id to the migration file or when you are generating the model. I do now know of a way for it to detect the association and create the user key at least from the generate line:

I believe your options are:

script/generate song group songname:string songtitle:string user_id:integer

Or in the migration - adding this to your migration file:

t.integer :user_id

Or also adding this to your migration file. I don't believe you can do this from the command line. And you will need to do it after you create the belongs_to :user association in your songs.rb model.

t.references :user

As a note, I generally take the second option.

One other aside just on general ruby/rails convention - generally attribute names are lowercase and separated by underscores. so song_name instead of songname. That is entirely a taste thing and up to you as to how you want to implement.

I hope this helps!

Geoff Lanotte