Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

after_validation callbacks are no longer executed if validation fails in rails3

#5419

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.

Reported by Jeremy Davis · August 20th, 2010 @ 07:07 AM

State: open
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • 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.

    August 20th, 2010 @ 02:51 PM

  2. Neeraj Singh
    Neeraj Singh
    • 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.

    August 20th, 2010 @ 03:22 PM

  3. Jeremy Davis
    Jeremy Davis

    Spectacular, thank you.

    August 21st, 2010 @ 08:51 AM

  4. José Valim
    José Valim
    • State changed from open to committed

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

    August 21st, 2010 @ 12:00 PM

  5. Jeremy Kemper
  6. af001
    af001

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

    May 5th, 2011 @ 02:55 AM

  7. learn italian online
    learn italian online
    • 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.

    May 20th, 2011 @ 01:20 AM

  8. learn italian online
    learn italian online

    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.

    May 20th, 2011 @ 01:21 AM