↖️ Show all posts

Active Record and dup vs clone

Look what I stumbled across on StackOverflow, there they say that:

When dealing with ActiveRecord there’s a significant difference too: dup creates a new object without its id being set, so you can save a new object to the database by hitting .save

 category2 = category.dup
#=> #<Category id: nil, name: "Favorites">

clone creates a new object with the same id, so all the changes made to that new object will overwrite the original record if hitting .save

category2 = category.clone
#=> #<Category id: 1, name: "Favorites">

⬅️ Read previous Read next ➡️