This project is archived and is in readonly mode.
ActiveRecord::Observer simplification causes callbacks to be fired multiple times
Reported by Kamal Fariz | February 9th, 2011 @ 05:13 PM
Commit 61f2d52d8 from #6065 causes the same callback from an observer to be fired multiple times.
For example,
class Developer < ActiveRecord::Base; end
class SpecialDeveloper < Developer; end
class DeveloperObserver < ActiveRecord::Observer
def before_save(developer)
puts "i'm called"
end
end
Calling SpecialDeveloper.create(:name => "foo")
will cause "i'm called"
to be printed twice.
The problem is the simplication in AR::Observer
created separate anonymous callbacks on each descendent
SpecialDeveloper.before_save { |record| observer.before_save(record) }
Developer.before_save { |record| observer.before_save(record) }
causing each to run in turn. Attached is a fix that goes back to
defining a method on Developer
and
SpecialDeveloper
and creating the callback of the
form
Developer.before_save :_notify_developer_observer_before_save_callback
SpecialDeveloper.before_save :_notify_developer_observer_before_save_callback
Comments and changes to this ticket
-
Jesse Storimer February 9th, 2011 @ 07:27 PM
I'm experiencing this bug as well. I don't see how it could be accomplished using the anonymous callbacks so falling back to the previous implementation seems best.
Applied cleanly with test passing against 3-0-4-security branch.
+1
-
Daniel Abrahamsson February 22nd, 2011 @ 07:24 AM
I have been unwilling to upgrade to 3.0.5 because of this bug.
+1
-
Aaron Patterson February 23rd, 2011 @ 12:57 AM
- State changed from new to committed
- Importance changed from to Low
Applied and pushed. Thanks!
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>