# How to change one attribute (or a group of attributes) when another changes?

**URL:** <https://www.ruby-forum.com/t/how-to-change-one-attribute-or-a-group-of-attributes-when-another-changes/188719>\
**Category:** Rails\
**Created:** [May 17, 2010, 9:24am UTC](https://www.ruby-forum.com/t/how-to-change-one-attribute-or-a-group-of-attributes-when-another-changes/188719 "2010-05-17T09:24:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 17, 2010, 9:24am UTC](https://www.ruby-forum.com/t/how-to-change-one-attribute-or-a-group-of-attributes-when-another-changes/188719/1 "2010-05-17T09:24:05Z")

</div>

I have a hosts class with an ip\_addresses class which has a host\_id  
attribute. All hosts are associated with a data\_center, and all  
ip\_addresses are associated with a network. Networks have a standard  
IP network definition (192.168.59.0/24) etc. When a user changes that  
network definition, I need to go through all ip\_addresses in the  
network and change them to agree with the new network definition. For  
example, if the network is defined as 192.168.59.0/24, and the user  
changes it to 10.168.59.0/24 in the networks form, I need to change  
all the ip\_addresses in that network to match the new scheme. So I  
need to know when the network changes. How do I do this? Like, is  
there a way to add code to the function that sets the network  
definition?

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 17, 2010, 6:33pm UTC](https://www.ruby-forum.com/t/how-to-change-one-attribute-or-a-group-of-attributes-when-another-changes/188719/2 "2010-05-17T18:33:47Z")

</div>

On May 16, 2010, at 6:20 PM, chewmanfoo wrote:

> there a way to add code to the function that sets the network  
> definition?

Add an ‘after\_save’ method to your Network model that checks to see  
what’s changed and if appropriate goes through and changes all of it’s  
IP\_Address children…

-philip

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 17, 2010, 11:21pm UTC](https://www.ruby-forum.com/t/how-to-change-one-attribute-or-a-group-of-attributes-when-another-changes/188719/3 "2010-05-17T23:21:47Z")

</div>

On 17 May 2010 02:20, chewmanfoo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> there a way to add code to the function that sets the network  
> definition?

I think you have redundant data in the db, as you are storing the ip  
address and the network definition, where much of the data is common  
between these two. I suggest not storing the full ip address, just  
the offset from that in the network definition, or something similar.  
Then your problem will go away.

Colin

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 17, 2010, 11:30pm UTC](https://www.ruby-forum.com/t/how-to-change-one-attribute-or-a-group-of-attributes-when-another-changes/188719/4 "2010-05-17T23:30:34Z")

</div>

Colin,

You’re right! But I can’t go down that path right now. In the future  
I’ll  
refactor the heck out of tis rats nest, but for now I have to make it  
work.

Jason

---

<div class="post-metadata">

**Author:** ![chewmanfoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c37758/32.png) [@chewmanfoo](https://www.ruby-forum.com/u/chewmanfoo)\
**Post date:** [May 18, 2010, 1:31am UTC](https://www.ruby-forum.com/t/how-to-change-one-attribute-or-a-group-of-attributes-when-another-changes/188719/5 "2010-05-18T01:31:31Z")

</div>

Jason M. wrote:

> Colin,
> 
> You’re right! But I can’t go down that path right now. In the future  
> I’ll  
> refactor the heck out of tis rats nest, but for now I have to make it  
> work.
> 
> Jason

If it’s a “rats nest”, it will probably be faster to refactor than to  
try to maintain it as it is.

## Best,

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