2017-10-18 41 views
0

我在rails應用程序模型中有兩種方法。爲類似AR查詢實現查詢對象的正確方法

def questions_mastered_for(user_id, question_group_ids) 
    res = UserAnswer 
      .joins('INNER JOIN question_group_questions ON user_answers.question_id = question_group_questions.question_id') 
      .joins('INNER JOIN marks ON user_answers.id = marks.user_answer_id') 
      .group('question_group_questions.question_group_id') 
      .where(question_group_questions: { question_group_id: question_group_ids }) 
      .where(user_answers: { user_id: user_id }) 
      .where("(marks.boolean_mark_correct = true AND marks.mark_type = 'boolean') OR (marks.fraction_numerator = marks.fraction_denominator AND marks.mark_type = 'fraction')") 
      .pluck('question_group_questions.question_group_id', 'COUNT(DISTINCT(user_answers.question_id))') 

    question_group_mastery = {} 
    question_group_ids.each { |question_group_id| question_group_mastery[question_group_id] = 0 } 
    res.each { |data| question_group_mastery[data.first] = data.second } 

    question_group_mastery 
end 

def questions_attempted_for(user_id, question_group_ids) 
    res = UserAnswer 
      .joins('INNER JOIN question_group_questions ON user_answers.question_id = question_group_questions.question_id') 
      .group('question_group_questions.question_group_id') 
      .where(question_group_questions: { question_group_id: question_group_ids }) 
      .where(user_answers: { user_id: user_id }) 
      .pluck('question_group_questions.question_group_id', 'COUNT(DISTINCT(user_answers.question_id))') 

    question_group_attempted = {} 
    question_group_ids.each { |question_group_id| question_group_attempted[question_group_id] = 0 } 
    res.each { |data| question_group_attempted[data.first] = data.second } 

    question_group_attempted 
end 

如果我們可以從上面的方法看,兩者幾乎做了類似的查詢,唯一的區別是在questions_mastered_for有額外的加入,where條件marks表。

我看了一些文章,通常這個特殊的用例可以使用查詢對象來解決,但我想知道如何正確實現它?

任何建議,我很感激。

回答

0
def questions_attempted_for_query(user_id, question_group_ids) 
    UserAnswer 
      .joins('INNER JOIN question_group_questions ON user_answers.question_id = question_group_questions.question_id') 
      .group('question_group_questions.question_group_id') 
      .where(question_group_questions: { question_group_id: question_group_ids }) 
      .where(user_answers: { user_id: user_id }) 
end 
def questions_mastered_for(user_id, question_group_ids) 
    res = questions_attempted_for_query(user_id, question_group_ids) 
      .joins('INNER JOIN marks ON user_answers.id = marks.user_answer_id') 
      .where("(marks.boolean_mark_correct = true AND marks.mark_type = 'boolean') OR (marks.fraction_numerator = marks.fraction_denominator AND marks.mark_type = 'fraction')") 
      .pluck('question_group_questions.question_group_id', 'COUNT(DISTINCT(user_answers.question_id))') 

    question_group_mastery = {} 
    question_group_ids.each { |question_group_id| question_group_mastery[question_group_id] = 0 } 
    res.each { |data| question_group_mastery[data.first] = data.second } 

    question_group_mastery 
end 

def questions_attempted_for(user_id, question_group_ids) 
    res = questions_attempted_for_query(user_id, question_group_ids) 
      .pluck('question_group_questions.question_group_id', 'COUNT(DISTINCT(user_answers.question_id))') 

    question_group_attempted = {} 
    question_group_ids.each { |question_group_id| question_group_attempted[question_group_id] = 0 } 
    res.each { |data| question_group_attempted[data.first] = data.second } 

    question_group_attempted 
end