Performance: CRUD operations through models
This affects how quickly data changes reflect on the page and the server response time during create, read, update, and delete actions.
Model.where(id: records.map(&:id)).update_all(attribute: new_value)records.each do |record| record.update(attribute: new_value) end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple individual updates in loop | Minimal DOM changes but delayed | 0 (server-side delay) | Low paint cost but delayed update | [X] Bad |
| Batch update_all query | Minimal DOM changes but faster | 0 (server-side delay) | Low paint cost with faster update | [OK] Good |
create adds a new record, find reads, update changes, and destroy deletes.create -> Option Acreate [OK]create [OK]find with createupdate to add new recordscreateupdate with a hash of attributes.user.update(email: 'new@example.com') uses symbol keys and colon, which is correct Ruby syntax.update with symbol keys and colon for attributes [OK]change or modifyemail => instead of email:user = User.find(1) user.destroy puts User.exists?(1)
destroy doesdestroy deletes the record from the database permanently.User.exists?(1) returns false because the user with ID 1 was deleted.destroy only marks record inactiveexists? returns true after deletiondestroy with delete (which skips callbacks)user = User.find_by(id: 10) user.update(name: 'Alice')
find_by behaviorfind_by returns nil if no matching record is found.update on nil causes a NoMethodError because nil has no update method.find_by returns nil if no record found, so calling update on nil causes error -> Option Cfind_by returns nil before calling methods [OK]find_by always returns a recordupdate cannot take hash argumentsfind and find_by behaviorUser.where(status: 'pending') selects only users with 'pending' status.update_all(status: 'active') updates the status for all filtered users in one database query.where to filter, then update_all to bulk update [OK]update_all before filteringfind_each which is slower for bulk updateswhere after update_allPass the test and this pattern moves unknown