Moving a record

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!

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. :slight_smile:

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

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. [email protected]

[email protected] wrote:

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

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