DirtyAssociations
Monitors changes to has_many
and has_and_belongs_to_many
associations using ActiveModel::Dirty.
Usage
Include DirtyAssociations
concern in your model.
include DirtyAssocations
Call monitor_association_changes
method after has_many
.
has_many :foos
monitor_association_changes :foos
Check one of the following methods to see if the relation has changed:
-
changes
-
changed
-
previous_changes
-
[singularized_association_name]_ids_changed?
-
[singularized_association_name]_ids_previously_changed?
.foos = [ Foo.find(42) ] # or you can use the foo_ids setter .changes # { "foo_ids" => [[5], [42]] } .changed # [ "foo_ids" ] .foo_ids_changed? # true .foo_ids_previously_changed? # false .save .previous_changes # { "foo_ids" => [[5], [42]] } .foo_ids_changed? # false .foo_ids_previously_changed? # true