This project is archived and is in readonly mode.
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
-
Rory O October 29th, 2009 @ 07:27 PM
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
-
Wesley Moxam October 29th, 2009 @ 07:52 PM
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'
-
Rory O October 29th, 2009 @ 07:59 PM
Consider this ticket me being a gigantic idiot. If I could pick myself being responsible, I would.
-
CancelProfileIsBroken October 29th, 2009 @ 10:36 PM
- State changed from new to invalid
We'll just make it go away then, shall we?
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>