This project is archived and is in readonly mode.

#5419 open
Jeremy Davis

after_validation callbacks are no longer executed if validation fails in rails3

Reported by Jeremy Davis | August 20th, 2010 @ 07:07 AM | in 3.0.2

In migrating our application from rails 2.3.x to rails 3 we have found that one of our post validation callbacks is being skipped. The callback is specifically concerned with reverting a changed record if the record was considered invalid.

I am including the callback method here for reference.

  after_validation :revert_status_if_validation_fails

  def revert_status_if_validation_fails
    if self.status_changed? && (self.draft? || !self.errors.empty?)
      self.status = self.status_was
    end
  end

Our expectation is that the after_validation callback will be run after validation regardless of the outcome of validation.

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh August 20th, 2010 @ 02:51 PM

    • Importance changed from “” to “Low”

    ActiveModel callbacks has following line of code.

            def after_validation(*args, &block)
              options = args.extract_options!
              options[:prepend] = true
              options[:if] = Array.wrap(options[:if])
              options[:if] << "!halted && value != false"
              options[:if] << "self.validation_context == :#{options[:on]}" if options[:on]
              set_callback(:validation, :after, *(args << options), &block)
            end
    

    Notice options[:if] << "!halted && value != false"

    It means that if validation returns false then do not call after_valdiation.

    I have updated the doc http://github.com/lifo/docrails/commit/c24aa2f2dca52e1b0ee644e40738...

    Whether after_validation should be called irrespective of the result is something that should be discussed with rails core team member.

  • Neeraj Singh

    Neeraj Singh August 20th, 2010 @ 03:22 PM

    • Milestone cleared.
    • State changed from “new” to “open”
    • Assigned user set to “José Valim”
    • Tag set to rails 3, activemodel, patch

    In 2.3.x after_validation callback is called irrespective of the result of valid? . I confirmed it and it was suggested by Jeremy Davis and hence the ticket.

    Attached is patch with test that fixes this issue in rails edge.

  • Jeremy Davis

    Jeremy Davis August 21st, 2010 @ 08:51 AM

    Spectacular, thank you.

  • José Valim

    José Valim August 21st, 2010 @ 12:00 PM

    • State changed from “open” to “committed”

    Ops, this patch was applied! Closing! Thanks Neeraj!

  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:02 PM

    • Milestone set to 3.0.2
  • af001

    af001 May 5th, 2011 @ 02:55 AM

    私の中で、総合評価のとっても低いアバアバクロホリスタークロ銀座店。アバクロは大好きなんですけどね。一昨日の東京駅付近での打ち合わせの後、散歩がてら久々に行ってきました。そしたらビックリ!相変わらアバクロず、踊っているだけの店員さんとかもいましたが、

  • learn italian online

    learn italian online May 20th, 2011 @ 01:20 AM

    • State changed from “committed” to “open”

    The resources of the Rails core team are Ray Ban 2140 limited, and so we are asking for your help. If you can still
    learn italian online reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. learn a language Likewise, if you feel that this is a very important feature for Rails to include, Ray Ban 3025 ple ase reply with your explanation so we can consider it.

  • learn italian online

    learn italian online May 20th, 2011 @ 01:21 AM

    If you saw Sunday's Free Rosetta Stone Language Press that shown Robert Bobb the emergency financial manager for Detroit Public rosetta stone Schools, move Mark Twain to rosetta Boynton which have three times the number seats then students and was one of the reason's he gave for closing school to many em rosetta stone spanish pty seats.

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>

Referenced by

Pages