Performance: Query interface basics
This affects how quickly the database queries run and how fast the page loads data from the database.
User.where(active: true)
User.all.select { |u| u.active? }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Loading all records then filtering in Ruby | N/A | N/A | Blocks rendering due to slow data fetch | [X] Bad |
| Filtering records in database query | N/A | N/A | Faster data fetch enables quicker rendering | [OK] Good |
where method do in Rails' query interface?wherewhere method is used to filter records by specifying conditions, like filtering users by age.delete, update, and create perform different actions, not filtering.where filters records [OK]created_at date in descending order?created_at: :desc.Product.where(price: 100).limit(3).pluck(:name), what will be the output?where(price: 100) filters products with price 100, limit(3) restricts to 3 records, pluck(:name) extracts names into an array.User.where('age > ?', 18).order('name ASC').limit('five')where('age > ?', 18) is correct, order('name ASC') accepts string, but limit('five') expects an integer, not a string.Article records published after 2020, sorted by title ascending, but only show the first 5 titles. Which query is correct?where('published_at > ?', Date.new(2020,1,1)). Article.where(published_at: > Date.new(2020,1,1)).order(:title).limit(5).pluck(:title) has invalid Ruby syntax. Article.where('published_at > 2020-01-01').order('title DESC').limit(5).pluck(:title) uses string date without quotes and wrong order direction.Pass the test and this pattern moves unknown