composite-primary-keys / composite_primary_keys

Composite Primary Keys support for Active Record
1.03k stars 350 forks source link

Wrong arity for uniqueness validate_each #578

Closed bf4 closed 2 years ago

bf4 commented 2 years ago

raise "wrong number of arguments (given 0, expected 1)"

triggered by a validation like

    validates :some_attribute, uniqueness: {
      conditions: ->(record) { 
          # some code
      }
  }

see

a patch to the validator would look like

    relation = scope_relation(record, relation)
    # BEGIN change from https://github.com/rails/rails/pull/39602
    # relation = relation.merge(options[:conditions]) if options[:conditions]
    if options[:conditions]
      conditions = options[:conditions]

      relation = if conditions.arity.zero?
        relation.instance_exec(&conditions)
      else
        relation.instance_exec(record, &conditions)
      end
    end
    # END change from https://github.com/rails/rails/pull/39602