Sha256: bab5f8e2ae619cbe30f7f348398c16898337a1e44ed009ade42d1b91640b2e1c
Contents?: true
Size: 1.22 KB
Versions: 38
Compression:
Stored size: 1.22 KB
Contents
require 'active_support' module ActiveRecord class Base # 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. def self.uniq_find_or_create_by(attributes, &block) find_or_create_by(attributes, &block) rescue ActiveRecord::RecordNotUnique => exception find_by(attributes) || raise(exception) end # 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. def self.uniq_find_or_create_by!(attributes, &block) find_or_create_by!(attributes, &block) rescue ActiveRecord::RecordNotUnique => exception find_by(attributes) || raise(exception) rescue ActiveRecord::RecordInvalid => exception find_by(attributes) || raise(exception) end end end
Version data entries
38 entries across 38 versions & 1 rubygems