Neil Neil - 1 year ago 35
Ruby Question

Make copy of record and save to database but with unique id

I am trying to make a new record that is an exact copy of an existing record, but I need the new record to of course to have a unique

. Meaning: I do not want to copy over the
from the existing record to the new record.

Current code

@blog = "some title")

@copy_of_existing_blog =

It returns this error:


I understand why it is erroring out. It is attempting to save a record that has a duplicate
of an existing record. I just do not know how to make an exact copy of a record, minus the
, and save it to the database.

max max

You need to filter out the ID from the attributes hash. ActiveSupport has a handy Hash#except method which does just this:"id"))

Additionally you may want to filter out the timestamps as well.