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?
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 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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With