I have another problem

finally I used

In Purchase model

def after_create
    product.update_attribute(:quantity, product.quantity + quantity)
end

and it works fine, but now I'm trying to do something else with the
update. I mean, sometimes you can input some wrong data, so you need
to update values. So I was thinking in update the purchase.quantity
and then automagically update the product.quantity, so I tried this

def before_update
    product.update_attribute(:quantity, product.quantity -
purchase.quantity)
  end
  def after_update
    product.update_attribute(:quantity, product.quantity +
purchase.quantity)
  end

but, that didn't work, in both cases purchase.quantity is the new
value, and I need in the first case (before_update) the original value
and in the second case (after_update) the new value for
purchase.quantity


On Jul 20, 6:21 pm, Angelo Cordova <acord...@gmail.com> wrote:
> Thanks for your help
>
> The last option worked for me
>
> def increase_product_purchase
>   product.update_attribute(:quantity, product.quantity + quantity)
> end
>
> it was pretty simple, but I couldn't figured out...
>
> Thanks again
>
> On Jul 20, 6:03 pm, Tom Meinlschmidt <to...@meinlschmidt.com> wrote:
>
>
>
>
>
>
>
> > God,
>
> > def increase_product_purchase
> >   product.increase_quantity(quantity)
> > end
>
> > in product
> > def increase_quantity(quantity)
> >   update_attribute(:quantity, number_of_purchases + quantity)
> > end
>
> > or simply in purchase
>
> > def increase_product_purchase
> >   product.update_attribute(:quantity, product.quantity + quantity)
> > end
>
> > ?
>
> > On Jul 20, 2011, at 23:55 , Angelo Cordova wrote:
>
> > > no, that's not what I'm looking for
>
> > > product.quantity is the current amount of products
> > > purchase.quantity is the amount of products I buy
>
> > > so the new product.quantity should be  the "old" product.quantity +
> > > purchase.quantity
>
> > > eg
>
> > > current product.quantity = 5
> > > purchase.quantity = 20
>
> > > new product.quantity = 25
>
> > > On Jul 20, 5:32 pm, Tom Meinlschmidt <to...@meinlschmidt.com> wrote:
> > >> do you have some relation here?
>
> > >> eg
>
> > >> product
> > >>   has_many :purchases
>
> > >> puchase (has column product_id)
> > >>   belongs_to :product
>
> > >> and in purchase.rb
> > >>   after_create :increase_product_purchase
>
> > >> def increase_product_purchase
> > >>   product.increase_purchase
> > >> end
>
> > >> and in product.rb
>
> > >> def increase_purchase
> > >>   update_attribute(:number_of_purchases, number_of_purchases + 1)
> > >> end
>
> > >> On Jul 20, 2011, at 23:08 , Angelo Cordova wrote:
>
> > >>> Hi people
>
> > >>> I have a doubt.
>
> > >>> I have 2 tables (models) "Product" and "Purchase". I want to use the
> > >>> callback after_create to update the attribute Product.quantity every
> > >>> time a Purchase is created, so I think this should be work, but I
> > >>> don't know the right syntax
>
> > >>> In purchase model
>
> > >>> def after_create
> > >>>         Product.update(product.quantity = product.quantity +
> > >>> purchase.quantity)
> > >>> end
>
> > >>> can somebody help me with this??
> > >>> 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@googlegroups.com.
> > >>> To unsubscribe from this group, send email to 
> > >>> rubyonrails-talk+unsubscr...@googlegroups.com.
> > >>> For more options, visit this group 
> > >>> athttp://groups.google.com/group/rubyonrails-talk?hl=en.
>
> > >> --
> > >> ===============================================================================
> > >> Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
>
> > >>www.meinlschmidt.comwww.maxwellrender.czwww.lightgems.cz
> > >> ===============================================================================
>
> > > --
> > > 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@googlegroups.com.
> > > To unsubscribe from this group, send email to 
> > > rubyonrails-talk+unsubscr...@googlegroups.com.
> > > For more options, visit this group 
> > > athttp://groups.google.com/group/rubyonrails-talk?hl=en.
>
> > --
> > ===============================================================================
> > Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
>
> >www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz
> > ===============================================================================

-- 
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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to