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.

Callbacks not being called with validations present

#3440

To reproduce, consider a Person basic model with an attribute of name:string, with the requisite DB table. Now add a failing callback to cause an error as such:

class Person < ActiveRecord::Base
before_create { raise } end

And this fails as expected:

dhcp80ff9b7b:ptest rory$ script/console
Loading development environment (Rails 2.3.4)

z = Person.create RuntimeError:
(...)

Perfect, but then change the class to:

class Person < ActiveRecord::Base
before_create { raise } validates_presence_of :name end
exit and reload the console:
dhcp80ff9b7b:ptest rory$ script/console
Loading development environment (Rails 2.3.4)

z = Person.create => #

before_create is never being called, therefore raise is never hit to trigger the test failure. All that is needed to reproduce is to use a basic class listed above. Further of note, when using .create with these conflicting conditions, a record is NOT created in the database:

Person.all => []

(note that in the console response, the id is nil)

The same behavior is also present when using before_create, after_create, before_save, after_save callbacks, including .create not creating a new record in the database.

I would track it down in the AR callbacks class, but I'm quite lost as to where to look there, apologies.

Reported by Rory O · October 29th, 2009 @ 07:24 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Rory O
    Rory O

    Sorry for the formatting, thought I had it

    A normal Person class that fails on creation:

    class Person < ActiveRecord::Base
      before_create { raise }
    end
    

    A failing Person class that doesn't hit raise as expected:

    class Person < ActiveRecord::Base
      before_create { raise }
      validates_presence_of :name
    end
    

    October 29th, 2009 @ 07:27 PM

  2. Wesley Moxam
    Wesley Moxam

    This is expected. Check the callback ordering: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

    What you are looking for is 'before_validation_on_create'

    October 29th, 2009 @ 07:52 PM

  3. Rory O
    Rory O

    Consider this ticket me being a gigantic idiot. If I could pick myself being responsible, I would.

    October 29th, 2009 @ 07:59 PM

  4. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to invalid

    We'll just make it go away then, shall we?

    October 29th, 2009 @ 10:36 PM