Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/roqua/core_ext/activerecord/uniq_find_or_create.rb
Class Method Summary collapse
-
.uniq_find_or_create_by(attributes, &block) ⇒ Object
Use this method to find or create records that have uniqueness constraints enforced by the database.
-
.uniq_find_or_create_by!(attributes, &block) ⇒ Object
Use this method if you want an exception to be raised when creating a new record fails due to some validation error other than uniqueness.
Class Method Details
.uniq_find_or_create_by(attributes, &block) ⇒ Object
Use this method to find or create records that have uniqueness constraints enforced by the database. After calling the AR find_or_create_by method it queries the preexisting or created record by the attributes provided, thereby ensuring that a concurrently created record is returned when a AR RecordNotUnique error is raised. When no record can be found, because for instance validations fail on create, the created object containing the validation errors is returned instead.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/roqua/core_ext/activerecord/uniq_find_or_create.rb', line 10 def self.uniq_find_or_create_by(attributes, &block) find_or_create_by(attributes, &block) # When a real race condition occurs, activerecord has no clue about a uniqueness constraint # being violated (this is exactly why validates :attribute, uniqueness: true does not work # for these cases) and a plain Mysql2::Error exception is raised instead of # ActiveRecord::RecordNotUnique rescue Mysql2::Error => exception find_by(attributes) || raise(exception) rescue ActiveRecord::RecordNotUnique => exception find_by(attributes) || raise(exception) end |
.uniq_find_or_create_by!(attributes, &block) ⇒ Object
Use this method if you want an exception to be raised when creating a new record fails due to some validation error other than uniqueness.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/roqua/core_ext/activerecord/uniq_find_or_create.rb', line 24 def self.uniq_find_or_create_by!(attributes, &block) find_or_create_by!(attributes, &block) # When a real race condition occurs, activerecord has no clue about a uniqueness constraint # being violated (this is exactly why validates :attribute, uniqueness: true does not work # for these cases) and a plain Mysql2::Error exception is raised instead of # ActiveRecord::RecordNotUnique rescue Mysql2::Error => exception find_by(attributes) || raise(exception) rescue ActiveRecord::RecordNotUnique => exception find_by(attributes) || raise(exception) rescue ActiveRecord::RecordInvalid => exception find_by(attributes) || raise(exception) end |