Skip to content

Instantly share code, notes, and snippets.

@michaelminter
Last active December 22, 2015 21:59
Show Gist options
  • Save michaelminter/6265b67d9aa9c8d498c5 to your computer and use it in GitHub Desktop.
Save michaelminter/6265b67d9aa9c8d498c5 to your computer and use it in GitHub Desktop.
SELECT `problems`.* FROM `problems` LEFT OUTER JOIN `subject_nodes` ON `subject_nodes`.`id` = `problems`.`subject_node_id` WHERE `problems`.`deleted` = 0 AND (`problems`.`problem_subject_id` = 74 AND `problems`.`public` = 1 AND `problems`.`problem_set_type` IN (1, 2, 4) AND 'IF(problems.problem_set_type=4 AND problems.subject_node_id IS NOT NULL, subject_nodes.parent_id, problems.id) IS NOT NULL')
WHERE IF(problems.problem_set_type=4 AND problems.subject_node_id IS NOT NULL, subject_nodes.parent_id, problems.id) IS NOT NULL
WHERE IF(problems.problem_set_type=4 AND problems.subject_node_id IS NOT NULL, subject_nodes.parent_id IS NOT NULL, '')
WHERE (CASE problems.problem_set_type=4 AND problems.subject_node_id IS NOT NULL THEN subject_nodes.parent_id IS NOT NULL)
scope :public_problems_in_subject, lambda { |problem_subject_id|
joins = arel_table.join(SubjectNode.arel_table, Arel::Nodes::OuterJoin).on(SubjectNode.arel_table[:id].eq(arel_table[:subject_node_id])).join_sources
query = arel_table[:problem_subject_id].eq(problem_subject_id)
.and(arel_table[:public].eq(true))
.and(arel_table[:problem_set_type].in([PROBLEM_TYPE_QUIZ, PROBLEM_TYPE_DIAGNOSTIC, PROBLEM_TYPE_CONCEPT]))
.and("(CASE problems.problem_set_type=#{PROBLEM_TYPE_CONCEPT} AND problems.subject_node_id IS NOT NULL THEN subject_nodes.parent_id IS NOT NULL END)")
joins(joins).where(query)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment