Rails Creates a model instance automatically when another

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

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 has nothing special
-feedback has user_id. This user_id must be the same as the user that was just created. For example, a 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 be 1. - The rating has feedback_id, and user_id user_id in this case is the identifier of the person who submitted rating. I am assigned the build command.

I believe my process is correct here.

purpose

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

Is there a better way to approach this? How to create a feedback model with the same ID as the created user when a new user is created. The idea is that when a user is created, a feedback associated with that user is created so people can navigate to

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

I am trying to work around if the user has another user with a ratings model because I am not sure how.

+2


a source to share


1 answer


Why not use the after_create callback and create a feedback in that method?



+3


a source







All Articles