# Simple things like duplicating a record in rails?

**URL:** <https://www.ruby-forum.com/t/simple-things-like-duplicating-a-record-in-rails/109933>\
**Category:** Rails\
**Created:** [July 20, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/simple-things-like-duplicating-a-record-in-rails/109933 "2007-07-20T12:20:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas\_Kviby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ad7895/32.png) [@Andreas\_Kviby](https://www.ruby-forum.com/u/Andreas_Kviby)\
**Post date:** [July 20, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/simple-things-like-duplicating-a-record-in-rails/109933/1 "2007-07-20T12:20:28Z")

</div>

Maybe I am totally waisted after a long nights work with my admin  
interface filters but how the heck can I duplicate records?

Scenario:  
My client will enter hundreds maybe thousands of products into their  
new nice e-commerce system backoffice forms. They call me and say,  
Hey! Can we press a “DUPLICATE” button would safe us hours and hours  
of time when adding products.

Solution?  
I can’t find any posts about this issues on the lists archives or  
anywhere else for that matter. I just want to have in my scaffolds  
next to show, edit, destroy a new link with the label “Duplicate and  
Edit” and if you press this link it should take the data on the  
selected product, copy it to a new product and go to the edit screen  
of that new product.

Any ideas on how to accomplish this?

---

<div class="post-metadata">

**Author:** ![Andreas\_Kviby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ad7895/32.png) [@Andreas\_Kviby](https://www.ruby-forum.com/u/Andreas_Kviby)\
**Post date:** [July 20, 2007, 12:38pm UTC](https://www.ruby-forum.com/t/simple-things-like-duplicating-a-record-in-rails/109933/2 "2007-07-20T12:38:33Z")

</div>

Andreas Kviby wrote:

> I can’t find any posts about this issues on the lists archives or  
> anywhere else for that matter. I just want to have in my scaffolds  
> next to show, edit, destroy a new link with the label “Duplicate and  
> Edit” and if you press this link it should take the data on the  
> selected product, copy it to a new product and go to the edit screen  
> of that new product.
> 
> Any ideas on how to accomplish this?

How about object.clone?

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![Andreas\_Kviby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ad7895/32.png) [@Andreas\_Kviby](https://www.ruby-forum.com/u/Andreas_Kviby)\
**Post date:** [July 20, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/simple-things-like-duplicating-a-record-in-rails/109933/3 "2007-07-20T12:43:35Z")

</div>

#controller  
def duplicate\_and\_edit  
@product = Product.find(params[:id]  
@product.id = nil  
end

if your form in new.rhtml is populated with form\_for @product … ,  
you could just render this action and rails will fill in the values  
into the form and thus reuse this view instead a creating a duplicate  
form for this action.

---

<div class="post-metadata">

**Author:** ![Andreas\_Kviby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ad7895/32.png) [@Andreas\_Kviby](https://www.ruby-forum.com/u/Andreas_Kviby)\
**Post date:** [July 20, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/simple-things-like-duplicating-a-record-in-rails/109933/4 "2007-07-20T13:40:05Z")

</div>

Hi  
Thanks a lot, it worked like a charm. Thanks to you guys my client  
will be very happy today. /Andreas

Read my blog at www.swerails.se
