This project is archived and is in readonly mode.

#6402 ✓committed
Kamal Fariz

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

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>

Attachments

Pages