alejoriveralara alejoriveralara - 7 months ago 13
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
clone.save


Even two lines would be better than three!

Answer

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

dup.tap { |clone| clone.assign_attributes(owner: owner) }.save