Duplicate Data But Have The Primary Keys Change With A Click

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.

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

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…

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

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:infind’
#{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:infind’
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:inperform_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:incall_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:inperform_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:inperform_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:insend’
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:inprocess_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:inprocess’
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:inprocess’
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:inprocess’
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:ineach’
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:inrun’
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:innew’
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:ininitialize’
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:inrun’
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:ineach’
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:inrun’
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:inload’
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:infind’
#{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:insend’
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:incall_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:inperform_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:inmeasure’
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:inperform_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:inprocess_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:inprocess’
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:indispatch’
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:insynchronize’
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:inprocess_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:inprocess_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:ininitialize’
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:inrun’
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:innew’
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:inrun’
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:inrun’
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:inrun’
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”}

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

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

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.

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!!!