has_many, belongs_to relation in active record migration rails 4

I have a User model and a Task model. I have not mentioned any relation between them while creating them.

I need to establish that User has_many Tasks and a Task belongs_to User through a migration

What would be the migration generation command for establishing that relationship?


Solution 1:

You could call:

rails g model task user:references

which will generates an user_id column in the tasks table and will modify the task.rb model to add a belongs_to :user relatonship. Please note, you must to put manually the has_many :tasks or has_one :task relationship to the user.rb model.

If you already have the model generated, you could create a migration with the following:

rails g migration AddUserToTask user:belongs_to

which will generate:

class AddUserToTask < ActiveRecord::Migration
  def change
    add_reference :tasks, :user, index: true
  end
end

the only difference with this approach is, the belongs_to :user relationship in the task.rb model won't be created automatically, so you must create it for your own.

Solution 2:

To answer the question, "What would be the migration generation command for establishing that relation?"( Meaning, how do you add a migration for existing models with a relationship like User has_many Tasks & Task belongs_to User)

The the easiest way for me to remember is like this:

>rails g migration AddUserToTask user:belongs_to

or

>rails g migration AddUserToTask user:references

:belongs_to is just an alias of :references, so either will do the same thing.

Doing it this way, the command will infer the name of the table from the migration name, set up a change method that will add the column for relationship, and configure it to be indexed:

class AddUserToTask < ActiveRecord::Migration
  def change
    add_reference :tasks, :user, index: true
  end
end

After generating that you:

>rake db:migrate

Finally, you still have to add the usual relations to your models, as is stated in the other answers, but I think this is the right answer to your question.

Solution 3:

This is how the migration should be created normally:

rails g scaffold child parent:references


I forgot to add parent:references when I created the table, what should I do?

Option 1: Destroy the table and start over

If you don't have a lot defined in the model/db about the child table. Your best bet might just be to run rails destroy scaffold child, and then run rails g scaffold child parent:references over it. Be sure to add the line drop_table :children if table_exists? :children before create table in the file that creates the new table. (That way if anyone pulls your code they can just run the migrations and be done.) However, it seems more probable that you will have data you don't want to lose in the child model already. In that case:

Option 2: Write a migration to add the references

rails g migration add_parent_refs_to_child

## XXXXXXXXXXXXXX_add_parent_refs_to_child.rb
class AddParentRefsToChild < ActiveRecord::Migration
  def change
    add_reference :child, :parent, index: true
  end
end

See add_reference

Additionally, don't forget to make sure the parent model has_[one | many] :children, and that the child model belongs_to :parent.


How not to do it:

You may be tempted to add the parent_id manually. Don't. Conventionally this sort of operation is handled through a migration, or within the initial table creation. Manual addition will detract from the maintainability of the project.

The Ruby on Rails guide to association has more information on the subject.

Solution 4:

There is no special migration command that would be used.

In your User model you will put

class User < ActiveRecord::Base
  has_many :tasks
end

class Task < ActiveRecord::Base
  belongs_to :user
end

In the corresponding migration file for the tasks you have the following field added user_id

Take a look at this guide