Skip to content

Instantly share code, notes, and snippets.

@ornerymoose
Created February 8, 2018 21:11
Show Gist options
  • Save ornerymoose/39b33252657462a253a38f5acdcf9d44 to your computer and use it in GitHub Desktop.
Save ornerymoose/39b33252657462a253a38f5acdcf9d44 to your computer and use it in GitHub Desktop.
trying to detect when the number of children an outage has changes
class Outage < ApplicationRecord
has_many :children
after_update :if_length_changed
def if_length_changed
o = Outage.find_by_id(self.id)
if (o.children.any?(&:changed?) || o.children.collect(&:id).sort != o.children.pluck(&:id).sort)
puts "\n\nlength was changed, count: #{o.children.count}\n\n"
else
puts "\n\nnotchanged, count: #{o.children.count}\n\n"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment