Module: FindOrCreateOnScopes
- Defined in:
- lib/find_or_create_on_scopes.rb
Overview
Adds ‘find_or_create`-type methods to named scopes.
Constant Summary collapse
- ABORT_SAVE =
Return this in a block from ‘find_or_create` or `create_or_update` to abort the creation/updating of a record.
Object.new
Instance Method Summary collapse
-
#create_or_update(*args) {|record| ... } ⇒ ActiveRecord::Base
Locates a record according to the current scope.
-
#create_or_update!(*args, &block) ⇒ Object
Same as #create_or_update but calls ‘save!` instead of `save` on the record.
-
#find_or_create(*args) {|record| ... } ⇒ ActiveRecord::Base
Locates a record according to the current scope.
-
#find_or_create!(*args, &block) ⇒ Object
Same as #find_or_create but calls ‘save!` instead of `save` on the record.
-
#find_or_initialize(*args, &block) ⇒ Object
Same as #find_or_create but does not save the record.
-
#initialize_or_update(*args, &block) ⇒ Object
Same as #create_or_update but does not save the record.
Instance Method Details
#create_or_update(*args) {|record| ... } ⇒ ActiveRecord::Base
Locates a record according to the current scope. Updates the record with the provided attributes if found. If not found, creates a new record with the current scope’s attributes and the provided attributes.
53 54 55 |
# File 'lib/find_or_create_on_scopes.rb', line 53 def create_or_update(*args, &block) create_or_update_and_do :save, *args, &block end |
#create_or_update!(*args, &block) ⇒ Object
Same as #create_or_update but calls ‘save!` instead of `save` on the record.
62 63 64 |
# File 'lib/find_or_create_on_scopes.rb', line 62 def create_or_update!(*args, &block) create_or_update_and_do :save!, *args, &block end |
#find_or_create(*args) {|record| ... } ⇒ ActiveRecord::Base
Locates a record according to the current scope. Returns the record if found. If not found, creates a new record with the attributes of the current scope and the provided attributes.
20 21 22 |
# File 'lib/find_or_create_on_scopes.rb', line 20 def find_or_create(*args, &block) find_or_initialize_and_do :save, *args, &block end |
#find_or_create!(*args, &block) ⇒ Object
Same as #find_or_create but calls ‘save!` instead of `save` on the record.
28 29 30 |
# File 'lib/find_or_create_on_scopes.rb', line 28 def find_or_create!(*args, &block) find_or_initialize_and_do :save!, *args, &block end |
#find_or_initialize(*args, &block) ⇒ Object
Same as #find_or_create but does not save the record. Please note that unless this method is called in a transaction, you might have a race condition when trying to save the record.
38 39 40 |
# File 'lib/find_or_create_on_scopes.rb', line 38 def find_or_initialize(*args, &block) find_or_initialize_and_do nil, *args, &block end |
#initialize_or_update(*args, &block) ⇒ Object
Same as #create_or_update but does not save the record. Please note that unless this method is called in a transaction, you might have a race condition when trying to save the record.
72 73 74 |
# File 'lib/find_or_create_on_scopes.rb', line 72 def initialize_or_update(*args, &block) create_or_update_and_do nil, *args, &block end |