Module: ActiveRecord::Locking::Pessimistic::ConvenienceMethods
- Defined in:
- lib/active_record/locking/pessimistic/convenience_methods.rb
Instance Method Summary collapse
-
#find_and_save_atomically(id, opts = {}, &block) ⇒ Object
Finds a records with the given ID, applies the block, then calls save.
-
#find_and_save_atomically!(id, opts = {}, &block) ⇒ Object
Same as find_and_save_atomically, just calls #save! on the model.
-
#find_and_update_atomically(id, attrs, opts = {}) ⇒ Object
Find and update attributes, but atomically.
Instance Method Details
#find_and_save_atomically(id, opts = {}, &block) ⇒ Object
Finds a records with the given ID, applies the block, then calls save. Returns the record.
All options are passed to #save, except for:
<tt>lock:</tt> - pass an SQL locking clause to append the end of the SELECT statement, defaults to "FOR UPDATE"
Example:
Post.find_and_save_atomically(1, validate: false, lock: "FOR SHARE") {|post| post.title = "new title" s}
17 18 19 |
# File 'lib/active_record/locking/pessimistic/convenience_methods.rb', line 17 def find_and_save_atomically(id, opts = {}, &block) _find_and_apply_atomically(:save, id, opts, &block) end |
#find_and_save_atomically!(id, opts = {}, &block) ⇒ Object
Same as find_and_save_atomically, just calls #save! on the model
22 23 24 |
# File 'lib/active_record/locking/pessimistic/convenience_methods.rb', line 22 def find_and_save_atomically!(id, opts = {}, &block) _find_and_apply_atomically(:save!, id, opts, &block) end |
#find_and_update_atomically(id, attrs, opts = {}) ⇒ Object
Find and update attributes, but atomically
27 28 29 |
# File 'lib/active_record/locking/pessimistic/convenience_methods.rb', line 27 def find_and_update_atomically(id, attrs, opts = {}) find_and_save_atomically(id, opts) {|r| r.assign_attributes attrs } end |