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