I have two models in my rails application. One is User that I have generated through with Devise and another one is Links. Now Initially There was only the Link model in my application. So I had two columns in my model Link namely “id” and “link” . Now my requirements have changed and I need implement a User model. A user in my application has one or many links. So added the line has_many :links to my “user.rb” and belongs_to :users to my “link.rb” file.
And I generated a migration like:
rails generate migration add_user_id_to_link user:references
That is, I’m generating a migration file to modify my existing table “links” and add a reference column “user_id” to the table.
But it’s giving an error when running rake db:migrate. Then I ran the command:
rails genearate migration add_user_id_to_links user_id:integer
But it’s giving a command not found error. Now the question is how do I achieve this??
Thanks in advance…
I’m pretty sure the references helper only works for
create_table. Which is why the first command doesn’t work.The second command should have worked though. The error is perhaps pointing to your typo in
generate.I hope that helps.