Method: Iry::Macros#unique_constraint
- Defined in:
- lib/iry/macros.rb
#unique_constraint(key_or_keys, name: nil, message: :taken, error_key: nil) ⇒ void
This method returns an undefined value.
Tracks uniqueness constraint for the given key (or keys) and convert constraint errors into validation errors
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/iry/macros.rb', line 97 def unique_constraint( key_or_keys, name: nil, message: :taken, error_key: nil ) keys = Array(key_or_keys) name ||= Constraint::Unique.infer_name(keys, table_name) error_key ||= keys.first if constraints.key?(name) raise ArgumentError, "Constraint already exists" end self.constraints = constraints.dup constraints[name] = Constraint::Unique.new( keys, message: , name: name, error_key: error_key ) end |