alejoriveralara alejoriveralara - 2 years ago 80
Ruby Question

How can I concisely duplicate an ActiveRecord object and change an attribute?

Can this be done in fewer lines?

clone = self.dup
clone.assign_attributes owner: owner

Even two lines would be better than three!

Answer Source

I don't know Rails but I assume this would work:

dup.tap { |clone| clone.assign_attributes(owner: owner) }.save
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download