Quirky aspects of Active Record
There are some things I find confusing about Active Record ORM/DSL (ORM or DSL or both?). I’m gonna write them down here so next time I get confused about them I have a place to go to and look them up:
1. .joins
is in fact an INNER JOIN
2. If you want a LEFT JOIN you need to use .left_joins
Which is also the same as .left_outer_joins
Rails docs for .left_joins
3. If you want a RIGHT JOIN you flip the .left_joins
Are there other ways?
4. If you want to join multiple associations, you use the comma
Book.joins(:author, :reviews)
Which produces:
SELECT books.* FROM books
INNER JOIN authors ON authors.id = books.author_id
INNER JOIN reviews ON reviews.book_id = books.id
5. If you want a NESTED association, you do NOT use the comma
Book.joins(reviews: :customer)
Which produces:
SELECT books.* FROM books
INNER JOIN reviews ON reviews.book_id = books.id
INNER JOIN customers ON customers.id = reviews.customer_id
Reference
And following paragraphs.
6. Use .includes
to help reduce N+1 queries, and eager load
Customer.includes(:orders, :reviews)
This loads all the customers and the associated orders and reviews for each.
Reference
And following paragraphs.
General post references:
- Active Record Basics
- Awesome blog post about Query Objects in Rails by Thiago - bookmark it for next time you are in scope hell.