Performance: Many-to-many with has_many through
MEDIUM IMPACT
This pattern affects database query performance and page load speed by controlling how many queries and joins are executed to fetch related data.
class Author < ApplicationRecord has_many :books_authors has_many :books, through: :books_authors end # In controller or view @authors = Author.includes(:books).all @authors.each do |author| author.books.each do |book| puts book.title end end
class Author < ApplicationRecord has_many :books_authors has_many :books, through: :books_authors end # In controller or view @authors.each do |author| author.books.each do |book| puts book.title end end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| N+1 queries without includes | Minimal DOM nodes | 1 reflow | Low paint cost | [X] Bad |
| Eager loading with includes | Minimal DOM nodes | 1 reflow | Low paint cost | [OK] Good |