Forum: Ruby on Rails Save as new?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ead43bcb0202410540a6a7cee5955d46?d=identicon&s=25 Fresh Mix (giga)
on 2009-03-03 16:45
@row = Post.find(5)

How can I save it as a new row?

Post.new(@row) ?? Do I need to destroy an old id?
E4681a514c48f1c6de50747be9d96d3f?d=identicon&s=25 Sébastien Dabet (fiddler)
on 2009-03-03 16:56
Fresh Mix wrote:
> @row = Post.find(5)
>
> How can I save it as a new row?
>
> Post.new(@row) ?? Do I need to destroy an old id?

Did you have a look at ActiveRecord::clone
(http://api.rubyonrails.org/classes/ActiveRecord/Ba...) ?

The following might work for you :

@row = Post.find(5)
clone = @row.clone
clone.save

(NB: The associations won't be cloned, only attributes)
This topic is locked and can not be replied to.