简体   繁体   English

如何使这项工作成为一个has_many:通过

[英]how to make this work as a has_many :through

I'm wondering if you can give me some advice on how to use a has_many :through association in this situation before I spend a whole lot of time rewriting my code to possibly no avail. 我想知道你是否可以给我一些关于如何使用has_many的建议:在这种情况下通过关联我花了很多时间重写我的代码可能无济于事。

I have a user model, and each user can play many quizzes (and therefore generate many quiz scores). 我有一个用户模型,每个用户可以玩很多测验(因此产生很多测验分数)。 Right now, the player starts a game by clicking on a user's name, which triggers a query for all the quiz questions that that user (a user has_many :questions) has made 现在,玩家通过点击用户名来启动游戏,这会触发对该用户(用户has_many:问题)所做的所有测验问题的查询

  def startGame 

     user = User.find_by_name(params[:user])
     questions = user.questions

  end 

For obvious reasons, a player can't play the same quiz twice. 出于显而易见的原因,玩家不能两次玩相同的测验。 Although I figured out a way to prevent that, I didn't do it an efficient way. 虽然我想出了一种方法来防止这种情况,但我没有采用有效的方法。 Someone suggested I try an association table. 有人建议我尝试一个关联表。 I started that association table as a has_many :through with this plan 我把这个关联表作为一个has_many :through这个计划

User.rb (this user is the user-as-player, not the user-as-quiz maker) User.rb(此用户是用户为播放器,而不是用户为测验的制造者)

has_many :scores
has_many :quizzes, :through => :scores

Quiz.rb Quiz.rb

   has_many :scores
   has_many :users    :through => :scores

Score 得分

   belongs_to :user
   belongs_to :quiz

The problem is that, currently, there is no quiz model or quiz anything. 问题是,目前没有测验模型或测验任何东西。 Quizzes are in fact just made up of questions (Question.rb) and answers (Answer.rb). 测验实际上只是由问题(Question.rb)和答案(Answer.rb)组成。 For example, each user has_many :questions and each question has_many :answers like this 例如,每个用户has_many :questions ,每个问题has_many :answers

User.rb User.rb

has_many :questions

Question.rb Question.rb

has_many :answers

So what can I make the Quiz model (or whatever I would call it) out of? 那么我可以将测验模型(或其他任何我称之为)的模型制作出来?

One idea that occured to me was to make a Quiz.rb model and do 发生在我身上的一个想法是制作一个Quiz.rb模型

has_many :questions

which would mean Question.rb belongs to both User.rb and Quiz.rb. 这意味着Question.rb同时属于User.rb和Quiz.rb.

Can I then do the has_many :through as described above with User, Quiz and Score? 我可以做has_many :through如上所述的用户,测验和分数? If so, Score will have both a user_id (for the player) and a quiz_id (for the quiz), but since the purpose of this whole association is to prevent users playing the same quiz twice, I'm wondering how I could make it work with the following code. 如果是这样,分数将同时具有user_id(对于玩家)和quiz_id(对于测验),但由于整个关联的目的是阻止用户两次播放相同的测验,我想知道我是如何做到的使用以下代码。

  def startGame

   user = User.find_by_name(params[:user])  #this user is the quiz creator
   questions = user.questions
     .....
    #I want to check right here if they've already played the quiz. how to do it with the has_many :through of Quiz, Score, User (as player)

  end

With the user.rb (player), score.rb, quiz.rb set up, how would I check if they've played this particular quiz in this situation. 设置了user.rb(player),score.rb,quiz.rb,如何在这种情况下检查他们是否已经玩过这个特定的测验。

Also, if I make the quiz model and do has_many :questions, would I would have to use polymorphism since user.rb also has_many :questions? 另外,如果我制作测验模型并执行has_many:问题,我是否必须使用多态,因为user.rb也有has_many:问题?

Update: I want to check whether they've already played in the def startGame method and not let them continue the game if they have. 更新:我想检查他们是否已经玩过def startGame方法,如果有的话,不要让他们继续游戏。 ie I don't want to wait until it's time to save the score to see if a score for that quiz has already been saved. 即我不想等到保存得分才能看到该测验的得分是否已经保存。

Your code looks very good already. 你的代码看起来非常好。 If you want to prevent any player from playing a quiz more than once, you can make any combination of a user and a quiz unique. 如果您想阻止任何玩家多次玩测验,您可以将用户和测验的任意组合设为唯一。 You can add this to your scores migration: 您可以将此添加到您的分数迁移中:

add_index :scores, [:user_id, :quiz_id], unique: true

And add this to your Scores model: 并将其添加到您的Scores模型中:

validates_uniqueness_of :user_id, scope: quiz_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM