as_json忽略.where()子句

时间:2017-03-27 21:53:36

标签: mysql ruby-on-rails ruby ruby-on-rails-4 activerecord

我正在尝试从包含5个表的现有数据库生成json结构: :用户 :资源 :quiz_questions :quiz_answers :quiz_responses

资源有很多quiz_questions,它有很多quiz_answers,反过来又有很多quiz_responses。用户还有_many quiz_responses。 (想法是用户进行多项选择测验并选择现有答案,然后在quiz_responses中创建新行。

所以我有两行代码:

questions = QuizQuestion.includes(:quiz_answers, :quiz_responses).where(resource_id: 623, quiz_responses: {user_id: 18276})

生成此查询:

SELECT 
  `quiz_questions`.`id` AS t0_r0,
  `quiz_questions`.`question` AS t0_r1
  `quiz_questions`.`resource_id` AS t0_r2, 
  `quiz_questions`.`created_at` AS t0_r3, 
  `quiz_questions`.`updated_at` AS t0_r4, 
  `quiz_questions`.`question_type` AS t0_r5, 
  `quiz_questions`.`url` AS t0_r6, 
  `quiz_questions`.`auto_next` AS t0_r7, 
  `quiz_questions`.`show_correct` AS t0_r8, 
  `quiz_questions`.`answer_type` AS t0_r9, 
  `quiz_answers`.`id` AS t1_r0, 
  `quiz_answers`.`answer` AS t1_r1, 
  `quiz_answers`.`quiz_question_id` AS t1_r2, 
  `quiz_answers`.`correct` AS t1_r3, 
  `quiz_answers`.`created_at` AS t1_r4, 
  `quiz_answers`.`updated_at` AS t1_r5, 
  `quiz_answers`.`answer_immediately` AS t1_r6, 
  `quiz_answers`.`time_limit` AS t1_r7, 
  `quiz_responses`.`id` AS t2_r0, 
  `quiz_responses`.`user_id` AS t2_r1, 
  `quiz_responses`.`quiz_answer_id` AS t2_r2, 
  `quiz_responses`.`created_at` AS t2_r3, 
  `quiz_responses`.`updated_at` AS t2_r4, 
  `quiz_responses`.`attempt_id` AS t2_r5, 
  `quiz_responses`.`video_url` AS t2_r6, 
  `quiz_responses`.`correct` AS t2_r7, 
  `quiz_responses`.`group_id` AS t2_r8 
FROM `quiz_questions` 
  LEFT OUTER JOIN `quiz_answers` ON `quiz_answers`.`quiz_question_id` = `quiz_questions`.`id` 
  LEFT OUTER JOIN `quiz_answers` `quiz_answers_quiz_questions_join` ON `quiz_answers_quiz_questions_join`.`quiz_question_id` = `quiz_questions`.`id` 
  LEFT OUTER JOIN `quiz_responses` ON `quiz_responses`.`quiz_answer_id` = `quiz_answers_quiz_questions_join`.`id` 
WHERE 
  `quiz_questions`.`resource_id` = 623 
  AND `quiz_responses`.`user_id` = 18276

第二行代码:

questions.as_json(include: { quiz_answers: { include: [:quiz_responses]}})

调用这些额外的查询:

  QuizResponse Load (0.8ms)  SELECT `quiz_responses`.* FROM `quiz_responses`  WHERE `quiz_responses`.`quiz_answer_id` = 755

  QuizResponse Load (0.8ms)  SELECT `quiz_responses`.* FROM `quiz_responses`  WHERE `quiz_responses`.`quiz_answer_id` = 756

  QuizResponse Load (1.5ms)  SELECT `quiz_responses`.* FROM `quiz_responses`  WHERE `quiz_responses`.`quiz_answer_id` = 757

  QuizResponse Load (0.7ms)  SELECT `quiz_responses`.* FROM `quiz_responses`  WHERE `quiz_responses`.`quiz_answer_id` = 758

  QuizResponse Load (0.6ms)  SELECT `quiz_responses`.* FROM `quiz_responses`  WHERE `quiz_responses`.`quiz_answer_id` = 759

  QuizResponse Load (0.7ms)  SELECT `quiz_responses`.* FROM `quiz_responses`  WHERE `quiz_responses`.`quiz_answer_id` = 760

  QuizResponse Load (0.6ms)  SELECT `quiz_responses`.* FROM `quiz_responses`  WHERE `quiz_responses`.`quiz_answer_id` = 761

  QuizResponse Load (0.6ms)  SELECT `quiz_responses`.* FROM `quiz_responses`  WHERE `quiz_responses`.`quiz_answer_id` = 764

  QuizResponse Load (0.8ms)  SELECT `quiz_responses`.* FROM `quiz_responses`  WHERE `quiz_responses`.`quiz_answer_id` = 765

此代码的预期目标是获取与资源867相关的所有问题,然后返回可能的答案以及与特定用途相关的响应(在本例中为18276)

问题在于,虽然只返回与资源867相关的问题as_json,但是尽管第一行中的where子句不是用户18276的响应,也会返回所有用户响应。为什么是这样?有什么方法我可以告诉as_json只使用它收到的内容并返回在初始查询中选择的quiz_responses?什么是“轨道方式”呢?

1 个答案:

答案 0 :(得分:1)

更新:新解决方案

Rails不允许预加载参数化关联。但是可以通过两个查询加载所有必要的数据并将其解析为json:

# load data
questions = QuizQuestion.preload(:quiz_answers)
answers_ids = questions.collect{ |question| question.quiz_answers.collect(&:id) }.flatten
responses = QuizResponse.where(quiz_answer_id: answers_ids).group_by(&:quiz_answer_id)

# making json
json_string = Jbuilder.encode do |json|
  json.array! questions do |question|
    json.merge! question.attributes

    json.quiz_answers question.quiz_answers do |answer|
      json.merge! answer.attributes

      json.quiz_responses responses[answer.id] do |response|
        json.merge! response.attributes
      end
    end
  end
end

如果问题中的用户没有quiz_response,则此代码不会从结果中省略quiz_answer。

第一个版本:

数据结构有两个quiz_responses关联:

QuizQuestion has_many :quiz_responses, through: :quiz_answers QuizAnswer has_many :quiz_responses 该查询包括quiz_responses作为QuizQuestion的关系。但as_json使用quiz_responses作为QuizAnswer的关系。并且Rails不够聪明,无法理解已经加载的必要的quiz_responses。

您需要做的就是以嵌套的方式重写包含,就像您想要使用它一样:

questions = QuizQuestion.includes(quiz_answers: :quiz_responses)