I have set of questions with question types as ["A", "B", "C", "D", "E", "F"], now i need to generate a game specific sets of questions ( 32 questions ) in a defined order
like for game 1 ["A", "A", "C", "A", "D", "A", "C", "D", "A", "A", "E", "F", .. ]
so far i have done following
def self.generate_rps_question(game_id)
questions = []
q1 = Question.where(game_id: game_id).where(qtype: "A").sample(1)
questions << q1 unless questions.include? (q1)
q2 = Question.where(game_id: game_id).where(qtype: "A").sample(1)
questions << q2 unless questions.include? (q2)
q3 = Question.where(game_id: game_id).where(qtype: "C").sample(1)
questions << q3 unless questions.include? (q3)
q4 = Question.where(game_id: game_id).where(qtype: "A").sample(1)
questions << q4 unless questions.include? (q4)
q5 = Question.where(game_id: game_id).where(qtype: "D").sample(1)
questions << q5 unless questions.include? (q5)
.
.
.
.
questions
end
Is there a better (shorter) way to do this ?
Update
def self.generate_rps_question(game_id, types)
types.inject([]) do |memo, type|
unless type == "F"
while memo.include?(
q = Question.where(game_id: game_id, qtype: type).sample) do end # skip unless unique
else
q = Question.where(game_id: game_id, qtype: type).sample
end
memo << q
end
end