Skip to content

Instantly share code, notes, and snippets.

@joshed-io
Created October 28, 2010 07:31
Show Gist options
  • Save joshed-io/650842 to your computer and use it in GitHub Desktop.
Save joshed-io/650842 to your computer and use it in GitHub Desktop.
class TouchParentObserver < ActiveRecord::Observer
observe MyModel, MyOtherModel
[:create, :update, :destroy].each do |action|
define_method("after_#{action}") do |record|
touch_parents_of_observed(record, action)
end
end
private
def touch_parents_of_observed(record, action)
unless record.skip_observation_on.include?(action)
record.send("observed_parents_to_touch").compact.each do |observed_parent|
observed_parent.update_attributes!(:updated_at, Time.now)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment