alejoriveralara alejoriveralara - 1 year ago 54
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