belongs_to association callbacks fixed in rails 2.3.3
Posted: 07/20/2009
Last night I noticed certain callbacks weren’t being called for belongs_to associations in Rails 2.3.2. Here is a simple example to demonstrate:
class Foo < ActiveRecord::Base has_many :bars end class Bar < ActiveRecord::Base belongs_to :foo end
I overwrote the callbacks in both of these classes to print its name upon being called. Take a look at the output using Rails 2.3.2:
Bar.create(:foo => Foo.new)
Bar before validation
Bar before validation on create
Bar validate
Bar validate on create
Bar after validation
Bar after validation on create
Foo before save
Foo before create
Foo after create
Foo after save
Bar before save
Bar before create
Bar after create
Bar after save
As you can see, none of the validation callbacks for Foo were called. Lucky for me, Rails 2.3.3 is out today and the problem seems to be fixed. Using the same models:
Bar.create(:foo => Foo.new)
Bar before validation
Bar before validation on create
Bar validate
Bar validate on create
Bar after validation
Bar after validation on create
Foo before validation
Foo before validation on create
Foo validate
Foo validate on create
Foo after validation
Foo after validation on create
Foo before save
Foo before create
Foo after create
Foo after save
Bar before save
Bar before create
Bar after create
Bar after save
Now they are all called!
Also, it appears that this problem was introduced in Rails 2.3.2. The example above produced the same results with Rails 2.2.2 as it did with 2.3.3.
I'm a skier, web developer, entrepreneur, freelancer, and all around stand-up guy living in Manhattan. This is me, repping one of my favorite shirts...