开发者

rails Creating a model instance automatically when another is created

开发者 https://www.devze.com 2022-12-27 00:14 出处:网络
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.

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 fr开发者_高级运维om 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消