-
-
Save balakirevs/a824b283f8b33e6843b3d2c76b502619 to your computer and use it in GitHub Desktop.
Awesome simple solution for Rails ActiveRecord dirty tracking associations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Credit Brandon Weiss of http://anti-pattern.com/dirty-associations-with-activerecord | |
# app/models/dirty_associations.rb | |
module DirtyAssociations | |
attr_accessor :dirty | |
attr_accessor :_record_changes | |
def make_dirty(record) | |
self.dirty = true | |
self._record_changes = record | |
end | |
def changed? | |
dirty || super | |
end | |
end | |
# app/models/lolrus.rb | |
class Lolrus | |
include DirtyAssociations | |
has_many :buckets, | |
:after_add => :make_dirty, | |
:after_remove => :make_dirty | |
end | |
# Usage | |
lol = Lolrus.create! | |
lol.buckets << Bucket.new | |
lol.changed? | |
# => true | |
lol._record_changes | |
# => #<Bucket# ... > | |
lol._record_changes.changed? | |
# => true | |
lol._record_changes.changes? | |
# for example ... | |
# => {[attr] => [old_value, new_value]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment