# Filter help when attribute changes to call procedure

**URL:** <https://www.ruby-forum.com/t/filter-help-when-attribute-changes-to-call-procedure/193371>\
**Category:** Rails\
**Created:** [August 16, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/filter-help-when-attribute-changes-to-call-procedure/193371 "2010-08-16T19:25:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [August 16, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/filter-help-when-attribute-changes-to-call-procedure/193371/1 "2010-08-16T19:25:45Z")

</div>

I have nested models:

Assessments has\_many Questions has\_many Answers.

I have plans on turning this into an engine where it can be used by  
other models.

There are several fields in both Questions and Answers that, if  
changed (or created ,deleted) I need to call a “update\_procedure” that  
does some calculations and saves the results in the Assessment.

Right now I have an after\_save and after\_destroy filter that calls the  
routine if anything is changed, which is not what I want. What  
approach do I need to handle filters if only certain attributes are  
change?. The reason is that I may need to change the state/status of  
the assessment if scores in another model were based on the current  
version. I don’t need to do the state change if the admin just fixed a  
misspelling.

I looked at Module: ActiveRecord::Dirty, but not sure I understand it.  
Is that what I need to understand?

---

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [August 16, 2010, 7:35pm UTC](https://www.ruby-forum.com/t/filter-help-when-attribute-changes-to-call-procedure/193371/2 "2010-08-16T19:35:09Z")

</div>

AppleII717 wrote:

> I have nested models:
> 
> Assessments has\_many Questions has\_many Answers.
> 
> I have plans on turning this into an engine where it can be used by  
> other models.
> 
> There are several fields in both Questions and Answers that, if  
> changed (or created ,deleted) I need to call a “update\_procedure” that  
> does some calculations and saves the results in the Assessment.
> 
> Right now I have an after\_save and after\_destroy filter that calls the  
> routine if anything is changed, which is not what I want. What  
> approach do I need to handle filters if only certain attributes are  
> change?. The reason is that I may need to change the state/status of  
> the assessment if scores in another model were based on the current  
> version. I don’t need to do the state change if the admin just fixed a  
> misspelling.
> 
> I looked at Module: ActiveRecord::Dirty, but not sure I understand it.  
> Is that what I need to understand?

A quick look at the rdoc would suggest that Dirty#changed would do what  
you want. An observer might be useful too.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [August 17, 2010, 12:10pm UTC](https://www.ruby-forum.com/t/filter-help-when-attribute-changes-to-call-procedure/193371/3 "2010-08-17T12:10:58Z")

</div>

I guess I forgot to mention this is rails3 and the edge rdoc threw me  
off.

In Rails 2.1+ there is ActiveRecord::dirty class, which looked like  
what I wanted. But in Rails 3 there is no ActiveRecord::dirty class.  
There is ActiveModel::dirty - and that is what I was referring to that  
I was not sure I understood.

After further digging, ActiveModel::dirty:

```
"Provides a way to track changes in your object in the same way as

```

Active Record does."

I am not sure what includes what, but there is  
ActiveRecord::AttributeMethods::Dirty, but I can’t seem to find the  
examples that were in the 2.x rdoc (e.g., person.name\_changed?).  
After some experimenting I found that the methods changed, changed?  
etc are there and work. I just can’t find the documentation.

Thank for the help.
