has_many association migration in Rails

11,707

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.

Share:
11,707
theDrifter
Author by

theDrifter

Updated on July 22, 2022

Comments

  • theDrifter
    theDrifter almost 2 years

    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?

    • Pavan
      Pavan almost 9 years
      You are doing it wrong. Add associations in model and corresponding fields in migration.
    • Pavan
      Pavan almost 9 years
      Just add associations in model.
    • Marcelo Toledo
      Marcelo Toledo about 6 years
      for me, the better answer for this question, was here: stackoverflow.com/a/17928074/4179050
  • theDrifter
    theDrifter almost 9 years
    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
  • Andrey Deineko
    Andrey Deineko almost 9 years
    @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).
  • theDrifter
    theDrifter almost 9 years
    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
    theDrifter almost 9 years
    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 :)