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?
To the best of my knowledge, you need to explicitly add the
user_idto 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:
Or in the migration – adding this to your migration file:
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 :userassociation in yoursongs.rbmodel.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_nameinstead ofsongname. That is entirely a taste thing and up to you as to how you want to implement.I hope this helps!