# New Model Object from Existing Model Object

**URL:** <https://www.ruby-forum.com/t/new-model-object-from-existing-model-object/65228>\
**Category:** Rails\
**Created:** [August 16, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/new-model-object-from-existing-model-object/65228 "2006-08-16T22:27:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 16, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/new-model-object-from-existing-model-object/65228/1 "2006-08-16T22:27:57Z")

</div>

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](http://www.nabble.com/New-Model-Object-from-Existing-Model-Object-tf2117592.html#a5839883)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 16, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/new-model-object-from-existing-model-object/65228/2 "2006-08-16T22:58:02Z")

</div>

On 8/16/06, sw0rdfish [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 16, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/new-model-object-from-existing-model-object/65228/3 "2006-08-16T23:22:04Z")

</div>

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  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](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](http://www.nabble.com/New-Model-Object-from-Existing-Model-Object-tf2117592.html#a5840904)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 16, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/new-model-object-from-existing-model-object/65228/4 "2006-08-16T23:25:04Z")

</div>

On 8/16/06, sw0rdfish [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [August 17, 2006, 12:22am UTC](https://www.ruby-forum.com/t/new-model-object-from-existing-model-object/65228/5 "2006-08-17T00:22:10Z")

</div>

That’s awesome… Thank you!

Jeremy K. wrote:

> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](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](http://www.nabble.com/New-Model-Object-from-Existing-Model-Object-tf2117592.html#a5841909)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).
