overridden validator not being used?

hello all,

i'm trying to override the validator validates_exclusion_of to make it case-insensitive:

module ActiveRecord::Validations::ClassMethods

  def validates_exclusion_of(*attr_names)     raise 'hello world'     configuration = { :message => ActiveRecord::Errors.default_error_messages[:exclusion], :on => :save }     configuration.update(attr_names.pop) if attr_names.last.is_a? (Hash)     enum = configuration[:in] || configuration[:within]

    raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?")     # make the compare case-insensitive     validates_each(attr_names, configuration) do |record, attr_name, value>       record.errors.add(attr_name, configuration[:message]) if enum.include?(value.downcase)     end   end

end

the code is in lib/validations.rb however when i run a unit test against it, i get the default validator instead of seeing raise "hello world"

what am i doing wrong?

thanks, jeff

jemminger wrote:

i'm trying to override the validator validates_exclusion_of to make it case-insensitive:

module ActiveRecord::Validations::ClassMethods   def validates_exclusion_of(*attr_names)     [OMITTED]   end end

the code is in lib/validations.rb however when i run a unit test against it, i get the default validator instead of seeing raise "hello world"

Active Record includes the validations module into the Base class. So you have to write

   class ActiveRecord::Base      def self.validates_exclusion_of(*attr_names)        [OMITTED]      end    end

Active Record includes the validations module into the Base class. So you have to write

   class ActiveRecord::Base      def self.validates_exclusion_of(*attr_names)        [OMITTED]      end    end

thanks for your reply. this still does not work. i've made sure that the file is being required in evironment.rb... out of curiosity, i also tried changing the method name to self.validates_exclusion_of2, and changed the reference in the model... i get a NoMethodError. i know that the file is being read, since if i put a raise before the method def i will get the raise.

any ideas?

actually there might be some strange problem just with this project... i tried your suggestion in a new test project and it works properly.

Ok, more information: we're using observers in environment.rb. if we comment out the line attaching the observers, all works properly. could this be a bug?

jemminger wrote:

Ok, more information: we're using observers in environment.rb. if we comment out the line attaching the observers, all works properly. could this be a bug?

Try putting the redefinition of the validator above the observer declaration in environment.rb.

no luck... the observers declaration is inside the Rails::Initializer.run block. any attempt to require the custom validations.rb before the end of that config block results in an error "uninitialized constant ActiveRecord"

i'll start digging through the rails source to see if i can find anything...

jemminger wrote:

no luck... the observers declaration is inside the Rails::Initializer.run block. any attempt to require the custom validations.rb before the end of that config block results in an error "uninitialized constant ActiveRecord"

Try seting the observers at the bottom of environment.rb like

ActiveRecord::Base.observers = :observer1, ...

Try seting the observers at the bottom of environment.rb like

ActiveRecord::Base.observers = :observer1, ...

shoot, i thought that this one was gonna do it but no - this time the validator worked properly but the observers don't get called.

jemminger wrote:

Try seting the observers at the bottom of environment.rb like

ActiveRecord::Base.observers = :observer1, ...

shoot, i thought that this one was gonna do it but no - this time the validator worked properly but the observers don't get called.

If you're in production, add "ActiveRecord::Base.instantiate_observers".

Alternatively, if you turn your patch into a plugin (just put your AR:B override in vendor/plugins/<something>/init.rb), that will automatically be loaded just before the observers are set-up in the initializer.

awesome... that works. thank you very much! maybe one day i'll understand or figure out why it didn't include the other way.