Question

I am designing a project management app and I want my users to be able to 'fork' an existing project in the database. For instance, @user1 will create a project and @user2 may want to modify that project, so @user2 will click an option to 'fork' it.
When that happens, the existing project will be pretty much duplicated for @user2, except he/she will be able to modify the contents of it.

I was thinking of that nested attributes might do the trick, but I don't think that is the correct method to go about this.

Any suggestions?

Was it helpful?

Solution

You should use the ActiveRecord::Core#dup method.
From the page:

Duped objects have no id assigned and are treated as new records. Note that this is a “shallow” copy as it copies the object’s attributes only, not its associations. The extent of a “deep” copy is application specific and is therefore left to the application to implement according to its need. The dup method does not preserve the timestamps (created|updated)_(at|on).

In short, @project.dup will build a new Project object with all parameters same as @project except - id, created_at, updated_at fields. @user2 can do modification on this newly build object and then save it. This mimics the fork feature.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top