Performance: Active Record pattern
This pattern affects database query performance and page load speed by how it fetches and manipulates data in Rails applications.
posts = Post.includes(:user).all posts.each do |post| puts post.user.name end
posts = Post.all posts.each do |post| puts post.user.name end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| N+1 queries without eager loading | Minimal DOM impact | No reflows caused by queries | Paint delayed by slow data fetch | [X] Bad |
| Eager loading with includes | Minimal DOM impact | No reflows caused by queries | Paint starts sooner due to faster data fetch | [OK] Good |
find fetches a record by its ID directly.User.find(5) uses find(5), which is correct. User.find_by_id(5) is deprecated. User.where(id: 5).first! is valid but less direct. User.get(5) is invalid.user = User.find(3) user.update(name: "Alice") puts user.name
update method changes the record's attributes and saves it.user.name is "Alice", so it prints "Alice".post = Post.find(10) post.title = "New Title" post.save!
where('created_at > ?', date) to filter by date.order(created_at: :desc) for descending order.Pass the test and this pattern moves unknown