This project is archived and is in readonly mode.

#3440 ✓invalid
Rory O

Callbacks not being called with validations present

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

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.

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