New Model Object from Existing Model Object

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 [email protected] wrote:

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

Help me out… what does clone = super do?

and .map(&:clone) would create new lines pointing to the same items?

Thanks in advance.

Jeremy K. wrote:

Items…
def clone
[email protected]
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 [email protected] 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

That’s awesome… Thank you!

Jeremy K. wrote:


Rails mailing list
[email protected]
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs