Advanced ActiveRecord Querying

The ActiveRecord query interface is an impressive feat of engineering, but often we only use a small portion of it. In this trail we'll dive deep into ActiveRecord and learn how to build complex queries taking advantage of eager loading, sub-selects, ordering across joins, and more!



In this course, we're going to go on a deep dive into ActiveRecord and teach you everything you need to know to manage complex relationships and queries. Joe Ferris, thoughtbot CTO and querying master, will be guiding us on our journey to help us take full advantage of ActiveRecord.

Sign In with GitHub for Free Access

Querying belongs_to Associations

Learn everything you need to work with your belongs_to relationships inside the database. Use joins, merge, and more to define concise, efficient queries that take full advantage of ActiveRecord's power.

Sign In with GitHub for Free Access

Querying belongs_to Associations Exercise

Put your new querying knowledge to the test with this exercise on querying belongs_to associations. Combine joins, merge, and the rest of tactics learned in the Querying belongs_to Associations video to lock in your new knowledge.

Querying has_many Associations

Learn advanced techniques for querying your `has_many` associations, including a deep dive into the `joins` method and how to use `merge` to compose queries and keep them logically separated.

Querying has_many Associations Exercise

Practice makes perfect, so dive into this exercise on querying has_many associations to lock in what you learned in the previous video. Use joins, distinct selects, and subqueries to query your has_many associations with ease.

Querying with Custom Joins

In some cases ActiveRecord just can't figure out the query you need, so you'll need to write it yourself. Luckily, it's not an all or nothing proposition. In this video learn how to use custom joins to answer complex questions about your database that ActiveRecord can't do for you.

Querying with Custom Joins Exercise

ActiveRecord can do a lot, but not everything. With this exercise, practice giving ActiveRecord a hand by writing custom SQL statements to define a join, all within the context of a larger ActiveRecord query.

Querying with Aggregations

After watching this video you'll attain querying master status. With this last installment, Joe teaches how to do some counts, minimums, maximums, averages, and sums. We'll even see how to perform these aggregations after grouping rows according to certain criteria and how to filter based on the results of these calculations, all via the database.

Querying with Aggregations Exercise

With this final exercise we'll bring everything together and even perform calculations and aggregations in the database. Complete this and you'll truly be a querying master.

Taught by
  • Chris Toomey

    Chris Toomey

    Chris is a big fan of Vim and the general Unix philosophy of sharp, focused tools. He started his career as a mechanical engineer but has always had a passion for programming, specifically web development. He has worked in Visual Basic and Python, but since finding Ruby and Rails, there’s been no looking back. Outside of coding, Chris is a fan of dinner with friends, skiing, and an embarrassingly eclectic cross section of music.

  • Joe Ferris

    Joe Ferris

    Joe provides technical leadership and support to the thoughtbot team. He has more than a hundred web applications under his belt and he's familiar with every reason you probably shouldn't use JavaScript. When he's not at his desk, you can generally find him outside, climbing something. Joe also enjoys interesting weather and type systems.