# How can I make an aggregated property support ActiveRecord::Dirty semantics?

**URL:** <https://www.ruby-forum.com/t/how-can-i-make-an-aggregated-property-support-activerecord-dirty-semantics/178559>\
**Category:** Rails\
**Created:** [November 6, 2009, 1:53pm UTC](https://www.ruby-forum.com/t/how-can-i-make-an-aggregated-property-support-activerecord-dirty-semantics/178559 "2009-11-06T13:53:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [November 6, 2009, 1:53pm UTC](https://www.ruby-forum.com/t/how-can-i-make-an-aggregated-property-support-activerecord-dirty-semantics/178559/1 "2009-11-06T13:53:29Z")

</div>

Hoping one of the many experts here might help out a newb…

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 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 semantics on  
aggregated attributes (they are on the underlying db attrs).

Is there a beautiful way to implement dirty semantics on the  
aggregation? I’m not having a lot of luck with  
alias\_attribute\_with\_dirty, etc. I really am unfond of making my own  
range\_changed?, range\_was, etc. Frankly, I was a bit surprised that  
aggregation didn’t support dirty natively…

Advice greatly appreciated.

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [November 6, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/how-can-i-make-an-aggregated-property-support-activerecord-dirty-semantics/178559/2 "2009-11-06T15:01:58Z")

</div>

On Nov 6, 12:51 pm, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So basically, I’m not getting ActiveRecord::Dirty semantics on aggregated attributes (they are on the underlying db attrs).
> 
> Is there a beautiful way to implement dirty semantics on the aggregation? I’m not having a lot of luck with alias\_attribute\_with\_dirty, etc. I really am unfond of making my own range\_changed?, range\_was, etc. Frankly, I was a bit surprised that aggregation didn’t support dirty natively…

The dirty stuff is all centred around actual attributes. you could  
define

def range\_changed?  
range\_begin\_changed? || range\_end\_changed  
end

def range\_was  
range\_begin … range\_end  
end

and so on. shouldn’t be too hard to automate definition of these  
methods.

Fred
