rails Creating a model instance automatically when another is created

Posted by bob on Stack Overflow See other posts from Stack Overflow or by bob
Published on 2010-04-14T02:25:41Z Indexed on 2010/04/14 2:33 UTC
Read the original article Hit count: 311

Hello I have a user model and a ratings model. Whenever a new user is created I want to create a new feedback model with it automatically.

Each user model has one feedback model and each feedback model has many ratings.

My Classes

class User < ActiveRecord::Base
end

class Feedback < ActiveRecord::Base
   belongs_to :user
   has_many :ratings
end

class Rating < ActiveRecord::Base
    belongs_to :feedback
end

My database tables

-user doesn't have anything special
-feedback has user_id. This user_id should be the same as the user that has just been created. For example, user_id of 1 is created, then a feedback model should be created that belongs to user_id of 1. So the user_id column in the feedback database will also be 1. - Rating has a feedback_id and a user_id the user_id in this case is the id of the person who submitted the rating. I am having it assigned through the build command.

I believe my process is correct here.

The Goal

The goal is to have each user have a feedback table that has many ratings from other users. So if someone goes to the feedback page, they will see all the ratings given and by who.

Is there a better way to approach this? How do you create a model of feedback with the same id as the user being created right when a new user is created. The idea is that when a user is created a feedback is created associated with that user so people can then go to

http://localhost:3000/users/1/feedback/ and submit new ratings.

I'm trying to bypass having a user rate another user with just a ratings model because I'm not sure how to do it.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about database