# Detect change to an associated attribute

**URL:** <https://www.ruby-forum.com/t/detect-change-to-an-associated-attribute/182726>\
**Category:** Rails\
**Created:** [February 2, 2010, 12:23am UTC](https://www.ruby-forum.com/t/detect-change-to-an-associated-attribute/182726 "2010-02-02T00:23:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerry\_Franks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/46a35a/32.png) [@Jerry\_Franks](https://www.ruby-forum.com/u/Jerry_Franks)\
**Post date:** [February 2, 2010, 12:23am UTC](https://www.ruby-forum.com/t/detect-change-to-an-associated-attribute/182726/1 "2010-02-02T00:23:54Z")

</div>

Consider these two models:

class Article \< ActiveRecord::Base  
has\_many :posts  
end

class Post \< ActiveRecord::Base  
belongs\_to :article  
end

The ActiveRecord::Dirty module allows one to detect changes in a  
model’s attribute. In the above example, how does one detect if the  
number of posts an article has changed, or the posts themselves have  
changed from the point of view of an Article object?

In other words, is there something like “article.posts\_changed?”?

---

<div class="post-metadata">

**Author:** ![Jerry\_Franks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/46a35a/32.png) [@Jerry\_Franks](https://www.ruby-forum.com/u/Jerry_Franks)\
**Post date:** [February 2, 2010, 12:30am UTC](https://www.ruby-forum.com/t/detect-change-to-an-associated-attribute/182726/2 "2010-02-02T00:30:44Z")

</div>

Here’s an example for you:

[http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects](http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects)

---

<div class="post-metadata">

**Author:** ![Jerry\_Franks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/46a35a/32.png) [@Jerry\_Franks](https://www.ruby-forum.com/u/Jerry_Franks)\
**Post date:** [February 2, 2010, 12:59am UTC](https://www.ruby-forum.com/t/detect-change-to-an-associated-attribute/182726/3 "2010-02-02T00:59:01Z")

</div>

I took a look at the referenced article but I don’t see where it  
addresses a has\_many association. What am I missing? Thanks.

---

<div class="post-metadata">

**Author:** ![Jerry\_Franks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/46a35a/32.png) [@Jerry\_Franks](https://www.ruby-forum.com/u/Jerry_Franks)\
**Post date:** [February 2, 2010, 2:02am UTC](https://www.ruby-forum.com/t/detect-change-to-an-associated-attribute/182726/4 "2010-02-02T02:02:14Z")

</div>

Hi Jerry,

I had this same problem, but dirty attributes don’t work for  
associations. I ended making my own plugin for it, called Dirty  
Associations, and it might be exactly what you’re looking for. You  
can get it here:

> **[GitHub - caseydreier/dirty\_associations: Adds dirty object-like behavior for...](https://github.com/caseydreier/dirty_associations)**
>
> Adds dirty object-like behavior for Rails model associations. - GitHub - caseydreier/dirty\_associations: Adds dirty object-like behavior for Rails model associations.

-Casey
