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.
Fresh M. (Guest)
on 2009-03-03 17: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?
Sébastien D. (Guest)
on 2009-03-03 17:56
Fresh M. 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.