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?

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
theDrifter
  • 1,631
  • 6
  • 24
  • 40

3 Answers3

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.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • 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 direction – theDrifter Jul 16 '15 at 10:33
  • 1
    @theDrifter 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). – Andrey Deineko Jul 16 '15 at 10:44
  • 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? – theDrifter Jul 16 '15 at 14:20
  • 1
    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 :) – theDrifter Jul 16 '15 at 15:17
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.

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.

mello
  • 21
  • 2
  • 5