# Moving a record

**URL:** <https://www.ruby-forum.com/t/moving-a-record/111951>\
**Category:** Rails\
**Created:** [August 13, 2007, 8:19pm UTC](https://www.ruby-forum.com/t/moving-a-record/111951 "2007-08-13T20:19:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 13, 2007, 8:19pm UTC](https://www.ruby-forum.com/t/moving-a-record/111951/1 "2007-08-13T20:19:45Z")

</div>

Hi!

I’d like to move a record from one table to another. I’ve been  
reading the documentation, and I think I can accomplish this by  
finding the record, creating a new record with those attributes, and  
then removing the first one. First, is there a better way to do this?

Next question, if I do go this way, do I have to specify every column  
that is going to be inserted into the database? For example, I would  
have to do this…

.new(:col1 =\> blah, :col2 =\> blah, :col3 =\> blah).save

or can I just use the results from the previous find and insert them  
into the .new() part? I tried to put the @results in  
(.new(@results).save), but that obviously didn’t work.

Thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 13, 2007, 9:09pm UTC](https://www.ruby-forum.com/t/moving-a-record/111951/2 "2007-08-13T21:09:45Z")

</div>

You could do it in one line:

NewModel.create(OldModel.find(id).destroy.attributes)

That provides no error checking or exception handling, of course. But  
I’m a sucker for oneliners. 🙂

Since it is destroying the old record before creating the new one, you  
may want to wrap it in a transaction. Or just expand it to more lines  
and ensure that the new record is created before destroying the old.  
However you prefer. The key is to use the attributes Hash.

-matthew

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 13, 2007, 11:18pm UTC](https://www.ruby-forum.com/t/moving-a-record/111951/3 "2007-08-13T23:18:11Z")

</div>

Ah, thank you very much… a little tweaking of the code and it  
worked.

MainArchive.create(Response.find(params[:id]).destroy.attributes).save

thanks for your help!!!

mike

On Aug 13, 2:09 pm, Matthew I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 13, 2007, 11:27pm UTC](https://www.ruby-forum.com/t/moving-a-record/111951/4 "2007-08-13T23:27:02Z")

</div>

removed\_email\_address@domain.invalid wrote:

> MainArchive.create(Response.find(params[:id]).destroy.attributes).save

You shouldn’t have to do that final save. create() saves for you.
