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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Sep 15, 7:06 am, Paolo <paolocastro....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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: >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 15, 4:14 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 15, 7:06 am, Paolo <paolocastro....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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: > > 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 ? > > FredThank you very much, i''m feeling kind a dumb right now, hehe. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---