Filter help when attribute changes to call procedure

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?

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
[email protected]

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.