Read more

Pitfall: ActiveRecord callbacks: Method call with multiple conditions

Martin Straub
April 24, 2013Software engineer at makandra GmbH

In the following example the method update_offices_people_count won't be called when office_id changes, because it gets overwritten by the second line:

after_save :update_offices_people_count, :if => :office_id_changed? # is overwritten …
after_save :update_offices_people_count, :if => :trashed_changed? # … by this line
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

Instead write:

after_save :update_offices_people_count, :if => :office_people_count_needs_update?

private

def office_people_count_needs_update?
  office_id_changed? || trashed_changed?
end

Or, move the conditions into the callback. This also allows you test the conditions more easily using a unit test:

after_save :update_offices_people_count

private

def update_offices_people_count
  if office_id_changed? || trashed_changed?  
    ...
  end
end

Also see the card on testing conditional validations.

Posted by Martin Straub to makandra dev (2013-04-24 16:09)