Ben Stephens
2005-Aug-04 14:01 UTC
updating has_and_belongs_to_many collection join attributes
Hi there, I would like to know how to update collection join attributes to add an item to a collection you can do push_with_attriubutes() e.g. order.products.push_with_attributes(product, :quantity = > 7) how do you update the quantity attribute in this collection? e.g. order.products.find(1).join_attributes(:quantity => 9) where join_attributes() is a fictitous method Any ideas, thanks, Ben
Ben Stephens
2005-Aug-08 08:45 UTC
Re: updating has_and_belongs_to_many collection join attributes
*Bump* Just to expand a little, order = Order.find(:first) #=> order inspection stuff order.products[0].quantity #=> 3 order.products[0].quantity = 7 #=> 7 order #=> a whole load of stuff that also shows that quantity is now 7 order.save #=> true order = Order.find(:first) order.products #=> shows quantity unchanged should i give up and 1) delete all the existing products and then add all of them back with new quantities or 2) create a new model object for orders_products On 8/4/05, Ben Stephens <foolfodder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi there, > I would like to know how to update collection join attributes > > to add an item to a collection you can do push_with_attriubutes() e.g. > order.products.push_with_attributes(product, :quantity = > 7) > > how do you update the quantity attribute in this collection? e.g. > order.products.find(1).join_attributes(:quantity => 9) > where join_attributes() is a fictitous method > > Any ideas, > thanks, Ben >
Caleb Buxton
2005-Aug-09 21:31 UTC
Re: Re: updating has_and_belongs_to_many collection join attributes
*Bump* and more (i share a similar problem)> *Bump* > > Just to expand a little, > order = Order.find(:first) #=> order inspection stuff > order.products[0].quantity #=> 3order.products.first.quantity.next! #=> 4 order.products.first.save! #=> true order.products(true).first.quantity #=> 3 or order.products.first.update_attributes(:quantity=>4) #=> true order.products(true).first.quantity #=> 3 AFAIK the only way to do this in AR with only a habtm association would be aprod = order.products.first order.products.delete(aprod) aprod.quantity.next! order.products << aprod or, to set up triggers on the DB end that would catch duplicate entry type stuff an update the attributes Has anyone already solved updating rich attributes in habtm with a single hit to the database rather than a delete and insert?
Michael Koziarski
2005-Aug-09 21:46 UTC
Re: Re: updating has_and_belongs_to_many collection join attributes
> Has anyone already solved updating rich attributes in habtm with a > single hit to the database rather than a delete and insert?If you need to update the attributes, introduce a model for the association. Updates aren''t supported on habtm attributes, nor will they be for the forseeable future. -- Cheers Koz
Caleb Buxton
2005-Aug-09 23:20 UTC
Re: Re: updating has_and_belongs_to_many collection join attributes
> If you need to update the attributes, introduce a model for the > association. Updates aren''t supported on habtm attributes, nor will > they be for the forseeable future.So would this be the best practice? class OrdersProducts < ActiveRecord::Base def self.increment_quantity!(product) OrdersProducts.update_all(["quantity=?",product.quantity.next!],["order_id=? and product_id=?",product.order_id,product.id]) end end class Products < ActiveRecord::Base ... def increment_quantity! if respond_to?(:quantity) && respond_to?(:scheduled_shift_id) QualificationsScheduledShifts.increment_quantity!(self) else #errors end end end
Caleb Buxton
2005-Aug-09 23:45 UTC
Re: Re: updating has_and_belongs_to_many collection join attributes
> class Products < ActiveRecord::Base > ... > def increment_quantity! > if respond_to?(:quantity) && respond_to?(:scheduled_shift_id) > QualificationsScheduledShifts.increment_quantity!(self) > else > #errors > end > end > endOf course... by QualificationsScheduledShifts I do mean OrdersProducts ... heh