This project is archived and is in readonly mode.

#6697 new
jwkpiano1

Validations' if blocks are run before any :on checks are made

Reported by jwkpiano1 | April 13th, 2011 @ 01:59 AM

In Rails 3, if you have both an :if block and an :on declaration on the same validation, the :if block runs before the :on declaration is checked. For example, in the following code:
validates_presence_of :blah, :on => :update, :if => lambda { |record| record.id == 4 } the :if block is run even if the context is a create.
This seems to be due to the following code in activemodel/validations/callbacks.rb:
`def before_validation(*args, &block) options = args.extract_options! if options.is_a?(Hash) && options[:on]

 options[:if] = Array.wrap(options[:if])
 options[:if] << "self.validation_context == :#{options[:on]}"

end set_callback(:validation, :before, *(args << options), &block) end`, where the check is added to the end of the options[:if] array, causing the check to run after the :if block.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages