Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 498 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to reflect in the database a new belongs_to and has_many relationship in Ruby on Rails

#1
I am new to rails (usually a python guy) and have just been trying to build a simple task manager application for fun. I am using Devise for authentication and have a single Task object I am trying to relate to a user. I have added the following to the Task model:

class Task < ActiveRecord::Base
belongs_to :user
end

and I have added the following in my User model for Devise:

class User < ActiveRecord::Base
has_many :tasks

<<normal Devise stuff>>
end

Whenever I added this information I then ran: rake db:migrate. It then gave me an error that the database field did not exist for user_id when I tried to do anything with it.

I am sure it is something rather simple that I am missing. Thanks for the help.
Reply

#2
Adding a `belongs_to` (or any other) relationship to your model only tells active record that models are linked logically. This gives you access to methods like `task.user`. For this to actually work, the instances must be linked via database fields.

This is the step you're missing: you need to create a migration that will add a column to the Task table indicating which user it belongs to.

rails g migration AddUserIdToTasks user_id:integer

Note AddUserIdToTasks can be whatever name you want. It makes no difference. You can then open `db/migrations/add_user_to_tasks` and see what it does. Usually self.up will modify the database how you want it and self.down will do the opposite (so, in this case, remove the used_id).

Then to actually execute the SQL commands to alter the database table and schema, run

rake db:migrate

Reply

#3
You need to generate a migration to add the foreign key first:

rails g migration AddUserIdToTasks user_id:integer

Then run `db:migrate`

And if you want the user to be able to reference the association as `user.dreams`, you need to add `:class_name => 'Task'` to the has_many line in the User model.
Reply

#4
Your user class seems to be too dreamy to take care of it's tasks.

has_many :dreams // should be probably
has_many :tasks

Assuming that you tasks model has a user_id field, that is.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through