How can I make an aggregated property support ActiveRecord::Dirty semantics?
I have an aggregated attribute which I want to be able ask about its _changed? ness, etc.
composed_of :range,
:class_name => 'Range',
:mapping => [ %w(range_begin begin), %w(range_end end)],
:allow_nil => true
If开发者_JAVA百科 I use the aggregation:
foo.range = 1..10
This is what I get:
foo.range # => 1..10
foo.range_changed? # NoMethodError
foo.range_was # ditto
foo.changed # ['range_begin', 'range_end']
So basically, I'm not getting ActiveRecord::Dirty semanitcs on aggregated attributes. Is there any way to do that? I'm not having a lot of luck with alias_attribute_with_dirty, etc.
You don't get ActiveRecord::Dirty semantics because aggregated attributes are not association proxies, being mere wrappers for your actual attributes - that's why you have no access to ActiveRecords goodness through them.
Note, that foo.range_begin_changed?
should work, so you may try to see if you can use foo.class#method_missing
(e.g. defined in a module to be included) to automatically map *_was
, *_changed?
etc methods to those called on actual attributes of foo.class
.
精彩评论