{"id":18336306,"url":"https://github.com/launchpadlab/rails_deep_copy","last_synced_at":"2026-03-03T06:37:12.363Z","repository":{"id":19786066,"uuid":"23045117","full_name":"LaunchPadLab/rails_deep_copy","owner":"LaunchPadLab","description":"Creates a deep duplicate of any active record object, its infinitely deep descendants, and reassigns their foreign keys appropriately.","archived":false,"fork":false,"pushed_at":"2017-01-30T21:41:19.000Z","size":47,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-21T17:23:18.829Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LaunchPadLab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-17T16:45:50.000Z","updated_at":"2024-02-12T14:44:16.000Z","dependencies_parsed_at":"2022-08-21T00:50:32.603Z","dependency_job_id":null,"html_url":"https://github.com/LaunchPadLab/rails_deep_copy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Frails_deep_copy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Frails_deep_copy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Frails_deep_copy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Frails_deep_copy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LaunchPadLab","download_url":"https://codeload.github.com/LaunchPadLab/rails_deep_copy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247435041,"owners_count":20938530,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T20:07:21.088Z","updated_at":"2026-03-03T06:37:12.330Z","avatar_url":"https://github.com/LaunchPadLab.png","language":"Ruby","readme":"Rails Deep Copy\n======================\n\nThe purpose of Rails Deep Copy is to provide an automated means of duplicating an ActiveRecord object with deeply nested children. The gem will recursively create these nested objects, ensuring that all foreign keys are appropriately synced to the newly generated copies.\n\n\n**Supported relationships:**\n\n* has_many\n* has_many :through\n* has_one\n* has_one :through\n\nUsage\n----------------------\n\n```ruby\n@project = Project.find(123)\n@project_copy = RailsDeepCopy::Duplicate.create(@project)\n```\n\n**Associated objects are automatically duplicated too!**\n```ruby\n@project.discussions.count\n#=\u003e 3\n@project.discussions.first.posts.count\n#=\u003e 6\n\n@project_copy.discussions.count\n#=\u003e 3\n@project.discussions.first.posts.count\n#=\u003e 6\n```\n\nHow It Works\n---------------------\n\nThe gem figures out which children objects need to be duplicated based on the associations on the model (has_many, has_one). For example, if a project \"has_many :discussions\" the gem would do the following:\n\n1. Create a copy of the project\n2. For each of the project's discussions, duplicate the discussion, change project_id to the new project's ID, and save the discussion\n\nStep 2 is actually done on a recursive basis, meaning the gem can handle deeply nested relationships. Consider the following: a project \"has_many :discussions\", a discussion \"has_many :posts\", and a post \"has_many :comments\". The gem will do the following:\n\nCreate a copy of the project. For each of the project's discussions, duplicate the discussion, change project_id to the new project's ID. For each of the discussion's posts, duplicate the post, change discussion_id, change project_id (if exists on model). For each of the post's comments, rinse and repeat the same steps, assigning project_id, discussion_id, and post_id when the attributes exist on the model.\n\n\nOptions\n----------------------\n\nRailsDeepCopy::Duplicate.create(object, options = {})\n\n**Options and their defaults:**\n* changes: {}\n* associations: {}\n* exclude_associations: {}\n* skip_validations: true\n\n```ruby\n@project = Project.find(122)\nRailsDeepCopy::Duplicate.create(@project, changes: {name: \"New Project's Name\", description: \"A great description\"}, skip_validations: false)\n```\n\n\nOverrides And Gotchas\n----------------------\n\n**Set Duplicable Associations**\n\nBeware of your relationships. The gem will use both the parent and descendant relationships to determine which objects to duplicate in the database. You can also override which associations are duplicable on your model like so:\n\n```ruby\nclass Project\n  has_many :discussions\n  has_many :posts\n  DUPLICABLE_ASSOCIATIONS = [:discussions]\n  # don't want to duplicate posts here since they are duplicated at the discussion level\n  # note that 'has_many :posts, through: :discussions' would solve this problem too\nend\n\nclass Discussion\n  has_many :posts\nend\n\nclass Post\n\nend\n\n```\n\n\n**Set Default Values for a Duplicated Object**\n\n```ruby\nclass Project\n  has_many :discussions\n  DUPLICABLE_DEFAULTS = {name: \"Project Copy\", description: \"A new awesome project\"}\nend\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaunchpadlab%2Frails_deep_copy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaunchpadlab%2Frails_deep_copy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaunchpadlab%2Frails_deep_copy/lists"}