Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 620
QuestionEverything Avatar asked Jul 27 '13 05:07

QuestionEverything


People also ask

How would you choose between Belongs_to and Has_one?

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.

What is polymorphic association in Rails?

In Ruby on Rails, a polymorphic association is an Active Record association that can connect a model to multiple other models. For example, we can use a single association to connect the Review model with the Event and Restaurant models, allowing us to connect a review with either an event or a restaurant.

Which command is true to rollback migration in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.

How do I migrate 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. rb , you would use 20160515085959 as the version number.


2 Answers

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.

like image 81
Alter Lagos Avatar answered Sep 17 '22 21:09

Alter Lagos


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.

like image 25
chad_ Avatar answered Sep 19 '22 21:09

chad_