Hey Guys, need a hand again.... Lets say I have an Order... and an Order has Order Lines and Order_Lines have Items... I want to create a New Order, which would have New Order Lines with the same Items... Is there a fast / easy way to do this, w/o having to create a new order, loop through all the order lines, and create them associating them with the Items? Any help would be great, thanks -- View this message in context: http://www.nabble.com/New-Model-Object-from-Existing-Model-Object-tf2117592.html#a5839883 Sent from the RubyOnRails Users forum at Nabble.com.
On 8/16/06, sw0rdfish <randal@b2blogic.net> wrote:> > Hey Guys, need a hand again.... > > Lets say I have an Order... and an Order has Order Lines and Order_Lines > have Items... > > I want to create a New Order, which would have New Order Lines with the > same > Items... > > Is there a fast / easy way to do this, w/o having to create a new order, > loop through all the order lines, and create them associating them with > the > Items?Perhaps class Order < ActiveRecord::Base def clone cloned = super cloned.lineitems = lineitems.map(&:clone) cloned end end jeremy -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060816/0fdb8986/attachment.html
Help me out... what does clone = super do? and .map(&:clone) would create new lines pointing to the same items? Thanks in advance. Jeremy Kemper wrote:> > On 8/16/06, sw0rdfish <randal@b2blogic.net> wrote: >> >> Hey Guys, need a hand again.... >> >> Lets say I have an Order... and an Order has Order Lines and Order_Lines >> have Items... >> >> I want to create a New Order, which would have New Order Lines with the >> same >> Items... >> >> Is there a fast / easy way to do this, w/o having to create a new order, >> loop through all the order lines, and create them associating them with >> the >> Items? > > > Perhaps > > class Order < ActiveRecord::Base > def clone > cloned = super > cloned.lineitems = lineitems.map(&:clone) > cloned > end > end > > jeremy > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- View this message in context: http://www.nabble.com/New-Model-Object-from-Existing-Model-Object-tf2117592.html#a5840904 Sent from the RubyOnRails Users forum at Nabble.com.
On 8/16/06, sw0rdfish <randal@b2blogic.net> wrote:> Help me out... what does clone = super do?super calls the superclass'' clone method. We get a cloned AR instance back. and .map(&:clone) would create new lines pointing to the same items? then we clone the original lineitems and assign them to the cloned instance. ActiveRecord::Base#clone copies the record''s attributes and removes its id, so it''s the same data ready to save anew. jeremy -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060816/2d52d7c7/attachment-0001.html
That''s awesome... Thank you! Jeremy Kemper wrote:> > On 8/16/06, sw0rdfish <randal@b2blogic.net> wrote: > >> Help me out... what does clone = super do? > > > super calls the superclass'' clone method. We get a cloned AR instance > back. > > and .map(&:clone) would create new lines pointing to the same items? > > > then we clone the original lineitems and assign them to the cloned > instance. > > ActiveRecord::Base#clone copies the record''s attributes and removes its > id, > so it''s the same data ready to save anew. > > jeremy > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- View this message in context: http://www.nabble.com/New-Model-Object-from-Existing-Model-Object-tf2117592.html#a5841909 Sent from the RubyOnRails Users forum at Nabble.com.