I am a Sr. Software Developer at Oracle Cloud. The opinions expressed here are my own and not necessarily those of my employer.
Cloning records in Rails
Nov 15, 2016
Sometimes we need to enable our users to clone their records instead of creating new ones from scratch (huge time saver). Let’s imagine a system where users have many accounts.
We can add more logic to clone_record to ensure uniqueness or transform data as needed. For basic CRUD UI I like using RailsAdmin. We can create custom action to call clone_record method.
Making GET request will load the clone page where user can confirm or cancel the action. This can be extended to cloning other records in the system. Just create clone_record method in the respective classes and add class name to register_instance_option :only do ....
Clone multiple records
We also might want to clone multiple records at a time. RailsAdmin supports bulk actions such as bulk_delete.
The differences from before are register_instance_option :collection do true end and register_instance_option :bulkable? do true end. Also there is bulk_clone UI page.
You can configure UI labels following this pattern. And here is bulk_delete template. Big thanks to this post for guidance on how to do custom bulk actions.