# How to use after\_save method to set fields on other models?

**URL:** <https://www.ruby-forum.com/t/how-to-use-after-save-method-to-set-fields-on-other-models/105094>\
**Category:** Rails\
**Created:** [May 25, 2007, 7:00pm UTC](https://www.ruby-forum.com/t/how-to-use-after-save-method-to-set-fields-on-other-models/105094 "2007-05-25T19:00:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@coelho](https://www.ruby-forum.com/u/coelho)\
**Post date:** [May 25, 2007, 7:00pm UTC](https://www.ruby-forum.com/t/how-to-use-after-save-method-to-set-fields-on-other-models/105094/1 "2007-05-25T19:00:56Z")

</div>

Hello,  
I am having some problems using the after\_save callback to update a  
field on another model, could anyone give me directions?  
Here is what I have:

class Model1 \< ActiveRecord::Base  
has\_many model2

after\_save :update\_field\_model2

def update\_field\_model2  
self.model2.field = (SomeStuff+somevariable) \* some\_other\_var  
end  
end

class Model2 \< ActiveRecord::Base  
belongs\_to model1  
end

What is the right way to update the value of the model2.field from  
model1?

thank you

---

<div class="post-metadata">

**Author:** ![coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@coelho](https://www.ruby-forum.com/u/coelho)\
**Post date:** [May 25, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/how-to-use-after-save-method-to-set-fields-on-other-models/105094/2 "2007-05-25T19:04:49Z")

</div>

just fixing it\*

class Model1 \< ActiveRecord::Base  
has\_one model2

…

---

<div class="post-metadata">

**Author:** ![coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@coelho](https://www.ruby-forum.com/u/coelho)\
**Post date:** [May 25, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/how-to-use-after-save-method-to-set-fields-on-other-models/105094/3 "2007-05-25T19:08:23Z")

</div>

coelho wrote:

> def update\_field\_model2  
> self.model2.field = (SomeStuff+somevariable) \* some\_other\_var  
> end  
> end
> 
> class Model2 \< ActiveRecord::Base  
> belongs\_to model1  
> end
> 
> What is the right way to update the value of the model2.field from  
> model1?

The way you do it looks fine to me. If you want to persist it you will  
of course have to do something like self.model2.save or  
self.model2.update\_attribute.

What is the issue you’re having?

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@coelho](https://www.ruby-forum.com/u/coelho)\
**Post date:** [May 25, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/how-to-use-after-save-method-to-set-fields-on-other-models/105094/4 "2007-05-25T19:47:20Z")

</div>

Yes, I wanted it to persist. It worked with model2.update\_attribute, I  
did not know about this method

thank you!

> The way you do it looks fine to me. If you want to persist it you will  
> of course have to do something like self.model2.save or  
> self.model2.update\_attribute.
> 
> What is the issue you’re having?
> 
> –  
> Cheers,
> 
> - Jacob A.
