# Duplicate Data But Have The Primary Keys Change With A Click

**URL:** <https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062>\
**Category:** Rails\
**Created:** [November 23, 2007, 11:16am UTC](https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062 "2007-11-23T11:16:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 23, 2007, 11:16am UTC](https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062/1 "2007-11-23T11:16:28Z")

</div>

I basically want to know whether there is such function on ruby on rails  
that allows me to duplicate a record but have the primary keys changed.  
For example, duplicate 20 records of Micheal Jackson’s address and  
details but the primary keys will be 1 to 20, I need this function for  
my database, is there something like this with a click of a button or a  
link, also a flash message that could tell me that i am on record number  
6 or 18 or 19 etc, thanks.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 23, 2007, 12:19pm UTC](https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062/2 "2007-11-23T12:19:56Z")

</div>

if your model is Article then  
your original article is in @article\_to\_copy

@copy = Article.create(@article\_to\_copy.attributes)

works with new, too

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 23, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062/3 "2007-11-23T13:06:56Z")

</div>

Thorsten M. wrote:

> if your model is Article then  
> your original article is in @article\_to\_copy
> 
> @copy = Article.create(@article\_to\_copy.attributes)
> 
> works with new, too

so my model is Artist, so in the artist new controller i should add:

@artist\_to\_copy = Artist.create(@artist\_to\_copy.attributes)  
@artist = Artist.new(artist\_to\_copy.attributes)

what do i put in the view I have not got alot of knowledge of HTML as i  
am still learning your answer would be welcomed thanks…

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 23, 2007, 1:30pm UTC](https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062/4 "2007-11-23T13:30:23Z")

</div>

> so my model is Artist, so in the artist new controller i should add:
> 
> @artist\_to\_copy = Artist.create(@artist\_to\_copy.attributes)  
> @artist = Artist.new(artist\_to\_copy.attributes)

no,  
@artist\_to\_copy = Artist.find( your find conditions, eg id )  
so you have the artist you want to copy  
then either  
@artist = Artist.create(@artist\_to\_copy.attributes)  
OR  
@artist = Artist.new(@artist\_to\_copy.attributes)  
@artist.save

> what do i put in the view I have not got alot of knowledge of HTML as i  
> am still learning your answer would be welcomed thanks…

in the view you simply put a link to your action, show your artists or  
whatever  
the link should hand over all necessary information to find the artist,  
your response should render the new state. sorry, but i can’t go into  
much more details here (i don’t know, what you want to show your users)  
maybe this will help you to get the basics:

[http://www.sitepoint.com/books/rails1/freebook.php](http://www.sitepoint.com/books/rails1/freebook.php)

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 23, 2007, 1:56pm UTC](https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062/5 "2007-11-23T13:56:10Z")

</div>

I have done all of this and it come back with an error:

ActiveRecord::RecordNotFound in ArtistsController#new

Couldn’t find Artist without an ID  
RAILS\_ROOT: C:/INSTAN~1/rails\_apps/example4/config/…  
Application Trace | Framework Trace | Full Trace

C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/base.rb:1012:in  
`find_from_ids' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:419:in`find’  
#{RAILS\_ROOT}/app/controllers/artists\_controller.rb:20:in `new’

C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/base.rb:1012:in  
`find_from_ids' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:419:in`find’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/base.rb:1095:in  
`send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in`perform\_action\_without\_filters’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/filters.rb:632:in  
`call_filter' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:634:in`call\_filter’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/filters.rb:619:in  
`perform_action_without_benchmark' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in`perform\_action\_without\_rescue’  
C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in`perform\_action\_without\_rescue’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/rescue.rb:83:in  
`perform_action' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in`send’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/base.rb:430:in  
`process_without_filters' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:624:in`process\_without\_session\_management\_support’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/session\_management.rb:114:in  
`process' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:330:in`process’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in  
`dispatch' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:78:in`process’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:76:in  
`synchronize' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:76:in`process’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:618:in  
`process_client' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in`each’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in  
`process_client' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in  
`initialize' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in`new’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in  
`run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in`initialize’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in  
`new' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:271:in  
`run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in`each’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in  
`run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:127:in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/command.rb:211:in  
`run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:243 C:/INSTAN~1/ruby/bin/mongrel_rails:16:in`load’  
C:/INSTAN~1/ruby/bin/mongrel\_rails:16

C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/base.rb:1012:in  
`find_from_ids' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:419:in`find’  
#{RAILS\_ROOT}/app/controllers/artists\_controller.rb:20:in `new' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in`send’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/base.rb:1095:in  
`perform_action_without_filters' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:632:in`call\_filter’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/filters.rb:634:in  
`call_filter' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:619:in`perform\_action\_without\_benchmark’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/benchmarking.rb:66:in  
`perform_action_without_rescue' C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in`measure’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/benchmarking.rb:66:in  
`perform_action_without_rescue' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/rescue.rb:83:in`perform\_action’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/base.rb:430:in  
`send' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in`process\_without\_filters’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/filters.rb:624:in  
`process_without_session_management_support' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/session_management.rb:114:in`process’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/base.rb:330:in  
`process' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in`dispatch’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:78:in  
`process' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:76:in`synchronize’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:76:in  
`process' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:618:in`process\_client’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in  
`each' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in`process\_client’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in  
`run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in`initialize’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in  
`new' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in  
`initialize' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in`new’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in  
`run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:271:in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in  
`each' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel\_rails:127:in  
`run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/command.rb:211:in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel\_rails:243  
C:/INSTAN~1/ruby/bin/mongrel\_rails:16:in `load’  
C:/INSTAN~1/ruby/bin/mongrel\_rails:16

Request

Parameters: None

Show session dump

* * *

flash: !map:ActionController::Flash::FlashHash {}

Response  
Headers: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 23, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062/6 "2007-11-23T15:19:00Z")

</div>

very funny,

@artist\_to\_copy = Artist.find( your find conditions, eg id )

must get an id, of course. like:

@artist\_to\_copy = Artist.find(1)

or

@artist\_to\_copy = Artist.find(params[:id])

if id was sent as param with the url

please read some tutorial that gives you at least an overview of basic  
RoR functionality

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 23, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062/7 "2007-11-23T15:28:00Z")

</div>

On 23 Nov 2007, at 12:30, Thorsten M. wrote:

> then either  
> @artist = Artist.create(@artist\_to\_copy.attributes)  
> OR  
> @artist = Artist.new(@artist\_to\_copy.attributes)  
> @artist.save
> 
> >

There’s also @artist\_to\_copy.clone

Fred

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 23, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062/8 "2007-11-23T16:17:49Z")

</div>

On Nov 23, 2007, at 4:16 AM, Nish P. wrote:

> For example, duplicate 20 records of Micheal Jackson’s address and  
> details but the primary keys will be 1 to 20

I think with Michael J. it would only go up to 12.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 23, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/duplicate-data-but-have-the-primary-keys-change-with-a-click/121062/9 "2007-11-23T17:54:47Z")

</div>

George B. wrote:

> On Nov 23, 2007, at 4:16 AM, Nish P. wrote:
> 
> > For example, duplicate 20 records of Micheal Jackson’s address and  
> > details but the primary keys will be 1 to 20
> 
> I think with Michael J. it would only go up to 12.

lol true say!!!, I have got it working now thanks!!!
