Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord::Observer simplification causes callbacks to be fired multiple times

#6402

Commit 61f2d52d8 from #6065 ActiveRecord::Observer is not aware of method added by including modules 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

Reported by Kamal Fariz · February 9th, 2011 @ 05:13 PM

State: committed
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Jesse Storimer
    Jesse Storimer

    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

    February 9th, 2011 @ 07:27 PM

  2. Daniel Abrahamsson
    Daniel Abrahamsson

    I have been unwilling to upgrade to 3.0.5 because of this bug.

    +1

    February 22nd, 2011 @ 07:24 AM

  3. Aaron Patterson
    Aaron Patterson
    • State changed from new to committed
    • Importance changed from to Low

    Applied and pushed. Thanks!

    February 23rd, 2011 @ 12:57 AM