Reputation: 9415
I am cloning an active record model (called Projects) using "dup". It has several associations; a project has many steps, steps have many images and videos, etc.
I'm finding that when I clone a project, it's successfully creating clones of the project's corresponding steps, images, and videos. However, the images and videos still have the step_ids of the original steps, not the newly created one. Is there an easy way for me to may the IDs to the newly created records?
I'm using the deep_cloneable gem.
This is how I'm creating my clone:
# Create remix
def remix
@project = Project.find(params[:id])
@remix_project = @project.dup :include => [:steps, :images, :videos]
@remix_project.remix = @project.id
@remix_project.title = @project.title+" - "+current_user.username+" Remix"
respond_to do |format|
if @remix_project.save
format.html { }
end
end
end
Upvotes: 3
Views: 1276
Reputation: 3282
When dealing with nested attributes, you need to change the format as follows:
@remix_project = @project.dup :include => {:steps => [:images, :videos]}
Upvotes: 3