alejoriveralara alejoriveralara - 5 months ago 8x
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!


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

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