has_many association migration in Rails
Asked Answered
F

3

9

I m working on a Rails project (Rails version 4.2.3). I created a User and Task model but did not include any association between them during creation. Now i want one user to have many tasks and one task belonging to one user.

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 the has_many migration? I updated the User 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 the has_many relationship?

Forepeak answered 16/7, 2015 at 9:28 Comment(3)
You are doing it wrong. Add associations in model and corresponding fields in migration.Motorboat
Just add associations in model.Motorboat
for me, the better answer for this question, was here: https://mcmap.net/q/273564/-has_many-belongs_to-relation-in-active-record-migration-rails-4Weatherspoon
I
24

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.

Indeed answered 16/7, 2015 at 9:43 Comment(4)
Hi 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 directionForepeak
@Forepeak if your tasks table already has user_id column, than as I've said, you only need to ensure associations on models levels(the very first part of my answer).Indeed
Hey @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?Forepeak
OK 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 :)Forepeak
I
1

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.

Insolvency answered 16/7, 2015 at 9:46 Comment(0)
E
1

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.

Ennead answered 9/5, 2020 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.