author avatar

giritharan

Fri May 10 2024

Dup vs clone while dealing with Active Record:
• Dup creates a new record with a blank ID, which will create a new object on the database with a new ID after hitting .save.


user = User.first
user1 = user.dup
user1:
<User id: nil, name: "Rails"> 


• clone creates a new record with the same ID, after hitting save on the clone object this will overwrite the existing data.


user = User.first
user1 = user.clone
<User id: 1, name: "Rails"> 


• If you change some value on the above attribute and put .save it will overwrite the original obj.
#dupvsclone #ruby #rails