Price: $25

Welcome to ActiveRecord vs SQL!

This course pragmatically teaches you a solid foundation to have more control over the SQL queries you write using the ActiveRecord ORM.

With real code examples, generated SQL, nuances and lesser-known tricks, you'll gain a deeper understanding of how ActiveRecord translates your Ruby code into SQL queries.

This knowledge is invaluable for optimizing database performance, writing efficient queries, and debugging complex database operations in your Rails applications.
Welcome to ActiveRecord vs SQL!

This course pragmatically teaches you a solid foundation to have more control over the SQL queries you write using the ActiveRecord ORM.

With real code examples, generated SQL, nuances and lesser-known tricks, you'll gain a deeper understanding of how ActiveRecord translates your Ruby code into SQL queries.

This knowledge is invaluable for optimizing database performance, writing efficient queries, and debugging complex database operations in your Rails applications.

Introduction

Understanding the relationship between ActiveRecord and SQL

Why understanding SQL is crucial for Rails developers

Course overview and setup

Book.where(title: "...") → equality filter

Basic equality filtering with where

How ActiveRecord translates this to SQL WHERE clauses

Multiple conditions and parameter binding

Book.where.not(...) → inequality and negation

Using where.not for negation

SQL NOT operator and its nuances

Handling NULL values in negations

Book.where(...).count → counting rows in SQL

Efficient counting with SQL COUNT()

Performance implications of count vs. length vs. size

Counting specific columns and expressions

Book.where(...).distinct → removing duplicates

Using DISTINCT in SQL queries

Performance considerations with distinct

Combining distinct with other query methods

Book.all → the base relation

Understanding ActiveRecord::Relation objects

Lazy loading and query execution

Building complex queries with method chaining

Book.first → first row by primary key

How first translates to ORDER BY and LIMIT in SQL

Default ordering by primary key

Customizing with first(n) and first!

Book.last → last row by primary key

How last translates to ORDER BY DESC and LIMIT in SQL

Reverse ordering considerations

Customizing with last(n) and last!

Book.find_by(...) → first match via WHERE ... LIMIT 1

Using find_by for single record retrieval

Differences between find, find_by, and where.first

Error handling with find_by vs. find_by!

Book.where(...).or(...) → combine predicates with OR

Using OR conditions in ActiveRecord queries

SQL OR operator and query optimization

Complex conditions with AND and OR combinations

Joins vs Preload vs Includes vs Eager load

Understanding the N+1 query problem

Comparing different eager loading strategies

When to use each loading strategy

includes with nested conditions → joined eager load

How includes automatically switches to joins

Filtering associated records with references

Complex nested conditions and their SQL translations

Group + Having + Count → books per author

Using group for data aggregation

Filtering grouped results with having

Advanced aggregation techniques

Book.merge → combine predicates

Using merge to combine conditions from different relations

Merging named scopes for cleaner code

Advanced merge techniques with arrays and procs

And many queries, etc..

Course summary and key takeaways

Advanced ActiveRecord and SQL topics to explore

Resources for continued learning


RubyCademy ©