Search code examples
ruby-on-railsvalidationactiverecordinstallationunique

Rails 4.1.0: undefined method `setup' for #<ActiveRecord::Validations::UniquenessValidator


I'm experiencing the following error when updating my Rails-app from 4.0.0 to 4.1.0 (Ruby 2.3.1):

undefined method `setup' for #<ActiveRecord::Validations::UniquenessValidator

The corresponding code is:

validator = ActiveRecord::Validations::UniquenessValidator.new({attributes: column, scope: scope})
    validator.setup(self.class)
    validator.validate(self)

This error seems to be known when upgrading to Rails 4.2. In Rails 4.1 the setup method should only be deprecated, as a result I'm really confused now. Trying to replace "setup" with "initialize" as recommended in this first answer's comment does not work (as I'm on Rails 4.1).

I can reliably reproduce the error by up and downgrading between Rails 4.0.0 and Rails 4.1.0.

Any help appreciated!


Solution

  • As mentioned here, you can achieve this by passing class as an argument while initialising validator instance,

    validator = ActiveRecord::Validations::UniquenessValidator.new({attributes: column, scope: scope, class: self.class})
    validator.validate(self)