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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tom Meinlschmidt
2011-Jul-20 21:32 UTC
Re: after_create callback and update trigger syntax
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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...-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Tom Meinlschmidt
2011-Jul-20 22:03 UTC
Re: Re: after_create callback and update trigger syntax
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...-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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...-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org> 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...-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org> 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > >>> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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...-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org> 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...-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org> 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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > >>> 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.