Has_many, belongs_to relation in active records migration rails 4

I created a User model and then a Task model. I did not mention any connection between them at creation.

I understand that User has_many Tasks and a Task belongs_to User . I need to establish this connection between them through migration.

My question is, what would be the migration generation team to establish this relationship?

Any help would be greatly appreciated.

+48
ruby-on-rails ruby-on-rails-4 rails-activerecord rails-migrations
Jul 27 '13 at 5:44 on
source share
6 answers

You may call:

 rails g model task user:references 

which generates the user_id column in the tasks table and task.rb model to add relatonship belongs_to :user . Note that you must manually set the has_many :tasks or has_one :task relationships to the user.rb model.

If you already have a generated model, you can 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 that the belongs_to :user relationship in the task.rb model task.rb not be created automatically, so you must create it for your own.

+57
Jul 29 '13 at 15:30
source share

To answer the question "What would be the migration generation team to establish this relationship?" (the meaning of how you add hyphenation for existing models with relationships such as User has_many Tasks and Task belongs_to User )

The easiest way to remember:

 >rails g migration AddUserToTask user:belongs_to 

or

 >rails g migration AddUserToTask user:references 

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

By doing this, the command will extract the table name from the migration name, configure the change method, which will add a column for the relationship, and configure it for indexing:

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

After creating this, you:

 >rake db:migrate 

Finally, you still have to add the usual relationships to your models as indicated in the other answers, but I think this is the correct answer to your question.

+23
Sep 22 '14 at 19:32
source share

How to do this when creating a migration in the first place:

rails g scaffold child parent:references




What if you forget the parent:references bit:

If you really have not decided on the / db model about the child. It would be best to run rails destroy scaffold child and then run rails g scaffold child parent:references above it. Be sure to add the line drop_table :children if table_exists? :children drop_table :children if table_exists? :children before creating a table in a file that creates a new table. (This way, if someone pulls your code, they can just start migrations and execute.) However, it seems more likely that you will have data that you do not want to lose in the child model.




In this case:

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 for more details.

Also, be sure to verify that the parent model has_[one | many] :children has_[one | many] :children and that the belongs_to :parent model belongs_to :parent .




How to do it:

You may be tempted to simply log in and add parent_id manually, and of course you might not be the best solution, as this is not the usual way to add foreign keys and is not well suited for maintainability or readability. Contract over configuration!

The Ruby on Rails Association Guide also provides more helpful information on this.

+15
01 Oct '14 at 2:57
source share

There is no special migration team to be used.

In your user model you place

 class User < ActiveRecord::Base has_many :tasks end class Task < ActiveRecord::Base belongs_to :user end 

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

Take a look at this guide.

+5
Jul 27 '13 at 6:01
source share

Migration will add the user ID to the task table so that they know about each other

 rails g migration AddUserIdToTask user_id:integer 

then

 rake db:migrate 

And after updating your controllers and views, so that tasks can not be created independently, but must correspond to the user

+4
Nov 18 '13 at 7:15
source share

Relations in Rails are handled by a model other than Rails.

Therefore, you just need to determine this ratio in your model:

 class User < ActiveRecord::Base has_many :tasks end class Task < ActiveRecord::Base belongs_to :user end 

And just make sure that during the migration in the field "user_id.

+3
Jul 27 '13 at 5:59 on
source share



All Articles