Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Last active February 25, 2016 20:58
Show Gist options
  • Save typeoneerror/9364182cd1ec5ee45946 to your computer and use it in GitHub Desktop.
Save typeoneerror/9364182cd1ec5ee45946 to your computer and use it in GitHub Desktop.
pry(main)> topics=Topic.distinct.joins('LEFT OUTER JOIN lessons ON lessons.topic_id = topics.id AND lessons.status = 1').preload(:lessons).where(course_id: 1)
Topic Load (0.8ms) SELECT DISTINCT "topics".* FROM "topics" LEFT OUTER JOIN lessons ON lessons.topic_id = topics.id AND lessons.status = 1 WHERE "topics"."course_id" = $1 ORDER BY "topics"."position" DESC, "topics"."id" ASC [["course_id", 1]]
Lesson Load (0.5ms) SELECT "lessons".* FROM "lessons" WHERE "lessons"."topic_id" IN (1, 2, 3, 4, 5, 18, 6, 23, 24) ORDER BY "lessons"."position" DESC, "lessons"."id" ASC
[109] pry(main)> topics.collect { |topic| [topic.title, topic.lessons.length] }
=> [["Introduction", 5],
["Basic Command Line Usage", 5],
["Customizing Terminal and Command Prompts", 3],
["Advanced Finding", 3],
["Networking", 4],
["Single Topic", 0],
["Tools", 3],
["Test", 0],
["No lesson module", 0]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment