# Recursive updates

**URL:** <https://www.ruby-forum.com/t/recursive-updates/148582>\
**Category:** Rails\
**Created:** [September 15, 2008, 9:40am UTC](https://www.ruby-forum.com/t/recursive-updates/148582 "2008-09-15T09:40:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@paolo](https://www.ruby-forum.com/u/paolo)\
**Post date:** [September 15, 2008, 9:40am UTC](https://www.ruby-forum.com/t/recursive-updates/148582/1 "2008-09-15T09:40:43Z")

</div>

Hi everybody, I’m trying to do some kind of recursive update that  
looks like this:

Products list  
Quantity Price Balance  
10 5 10  
5 5 15

as you can see, balance is like the current quantity + previous  
balnce, the problem is when i update a product, because the next  
product must be updated too, and so on. so far i tryed this:

class Product \< ActiveRecord::Base  
after\_update :update\_next\_product  
before\_update :prepare\_product

def prepare\_product  
unless actual\_index == 0  
self.balance = previous\_product.balance + income  
else  
self.balance = income  
end  
end

def update\_next\_product  
next\_product.update\_attributes({}) unless next\_product  
end

def next\_product  
Product.find(:all)[actual\_index+1] unless actual\_index ==  
(entries.size - 1)  
end

def previous\_product  
Product.find(:all)[actual\_index-1] unless actual\_index == 0  
end

def actual\_index  
Product.find(:all).index(self)  
end  
end

am I missing something?  
thanks

---

<div class="post-metadata">

**Author:** ![paolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@paolo](https://www.ruby-forum.com/u/paolo)\
**Post date:** [September 15, 2008, 10:15am UTC](https://www.ruby-forum.com/t/recursive-updates/148582/2 "2008-09-15T10:15:18Z")

</div>

On Sep 15, 7:06 am, Paolo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> product must be updated too, and so on. so far i tryed this:

I’m not sure entirely what you’re doing but you’re doing the update  
unless next\_product: surely that should be if next\_product ?

Fred

---

<div class="post-metadata">

**Author:** ![paolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6f9a4e/32.png) [@paolo](https://www.ruby-forum.com/u/paolo)\
**Post date:** [September 15, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/recursive-updates/148582/3 "2008-09-15T21:06:42Z")

</div>

On Sep 15, 4:14 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > as you can see, balance is like the current quantity + previous  
> > balnce, the problem is when i update a product, because the next  
> > product must be updated too, and so on. so far i tryed this:
> 
> I’m not sure entirely what you’re doing but you’re doing the update  
> unless next\_product: surely that should be if next\_product ?
> 
> Fred

Thank you very much, i’m feeling kind a dumb right now, hehe.
