Performance: Where conditions
This affects how quickly the database query returns results and how much data the server processes before sending to the browser.
User.where(active: true).where('age > ?', 30)
User.all.select { |user| user.active? && user.age > 30 }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Filtering in Ruby after loading all records | N/A | N/A | Blocks rendering due to slow data | [X] Bad |
| Filtering with ActiveRecord where conditions | N/A | N/A | Faster rendering due to less data | [OK] Good |
where method do in Rails ActiveRecord?wherewhere method is used to specify conditions to filter records in a database query.delete, migrations, or update_all.where?age: 30 is the standard way to filter by attribute value.User.where("name LIKE ?", "%John%")User.where(age > 25)
User.where("age > ?", 25) or User.where("age > 25").where conditions to find users who are active and older than 18?Pass the test and this pattern moves unknown