has_many association migration in Rails
Solution 1
Set up associations in models:
class User < ActiveRecord::Base
has_many :tasks
end
class Task < ActiveRecord::Base
belongs_to :user
end
Delete the migration file you've shown.
Add references to tasks
table (assuming you already have tasks
table):
rails g migration add_references_to_tasks user:references
Migrate the database:
rake db:migrate
If you don't have tasks
table yet, create one:
rails g migration create_tasks name due_date:datetime user:references # add any columns here
Migrate the database:
rake db:migrate
From now on your tasks will have user_id
attribute.
Solution 2
Add has_many :tasks
to the User
model and belongs_to :user
to the Task
model. In your migration file, delete all the current body of the change
method and include a add_index :tasks, :user_id
line. After that, run the migration normally.
Solution 3
I know this is an old thread but efforts are only to improve on this. I think what you were going for was to show reference foreign key in the table. In which case:
class addTasksToUser < ActiveRecords::Migration
def change
update_table :users do |t|
t.references :task
end
end
Please make sure your references to the table with the primary key is singular.
theDrifter
Updated on July 22, 2022Comments
-
theDrifter almost 2 years
I m working on a Rails project (Rails version 4.2.3). I created a
User
andTask
model but did not include any association between them during creation. Now i want oneuser
to have manytasks
and onetask
belonging to oneuser
.Through
rails g migration AddUserToTask user:belongs_to
from this thread i was able to insert the foreign user_id key in the tasks table. But how to i add a thehas_many
migration? I updated theUser
model:class User < ActiveRecord::Base has_many :customers end
but i m not sure how i have to write the migration. So far i wrote this:
class addTasksToUser < ActiveRecords::Migration def change update_table :users do |t| t.has_many :tasks end add_index :users, taks_id end end
But
rake db:migrate
is not performing any action. Is this the correct way to setup thehas_many
relationship?-
Pavan almost 9 yearsYou are doing it wrong. Add associations in model and corresponding fields in migration.
-
Pavan almost 9 yearsJust add associations in model.
-
Marcelo Toledo about 6 yearsfor me, the better answer for this question, was here: stackoverflow.com/a/17928074/4179050
-
-
theDrifter almost 9 yearsHi Andray thanks for you detailed answer. My tasks already have a user_id. How do i get the other way so, that my user have many tasks. I highly struggle implementing this direction
-
Andrey Deineko almost 9 years@theDrifter if your
tasks
table already hasuser_id
column, than as I've said, you only need to ensure associations on models levels(the very first part of my answer). -
theDrifter almost 9 yearsHey @Andrey thanks for your sicking with me :). I did setup the models accordingly. How do i get the user associated with a set of tasks and also translate it to my table structure?
-
theDrifter almost 9 yearsOK i got my mistake. I did not fully understand the many_to_one relationship. Now i want the tasks which belong to one user. I just call the tasks for a specific user_id. I thought i have to store each task_id in the user table which makes absolutely no sense :)