Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

has_many association migration in Rails

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?

like image 785
theDrifter Avatar asked Jul 16 '15 09:07

theDrifter


People also ask

What is the difference between Has_one and Belongs_to?

They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.

How do I migrate a specific migration in Rails?

How do I run a specific migration in rails? To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users.

What is difference between model and migration in Rails?

Rails Model (Active Record) works with SQL, and Rails Migration works with DDL. Rails Model supports ways to interact to with the database, while Rails Migration changes the database structure. A migration can change the name of a column in books table.

What is polymorphic association in Rails?

Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.


3 Answers

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.

like image 76
Andrey Deineko Avatar answered Oct 12 '22 07:10

Andrey Deineko


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.

like image 36
Otávio Monteagudo Avatar answered Oct 12 '22 07:10

Otávio Monteagudo


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.

like image 1
red-x Avatar answered Oct 12 '22 06:10

red-x