Michael Graff
Michael Graff

Reputation: 485

convert existing record to new record automatically

I am wondering if, in some hidden corner of the API I haven't yet run into, if there is a way to clone an existing record into a new one, so when saved it will have a new id assigned?

This is intended to be used on an event site I am writing, which will allow people to import from previous years, but copying it will allow updating the event description with new content.

Upvotes: 0

Views: 33

Answers (1)

vee
vee

Reputation: 38645

You can use dup method for this.

Given object user1 of model User, you can do:

user2 = user1.dup
user2.save

Doing user2 = user1.dup clones user1 into user2, and user2 has no id, created_at and updated_at values assigned and it is treated as a new record.

Upvotes: 2

Related Questions