Sunday, 11 August 2013

How to duplicate a record in Rails except for one attribute?

How to duplicate a record in Rails except for one attribute?

In my Rails application I have a method that duplicates an invoice
including its items.
class Invoice < ActiveRecord::Base
def duplicate
dup.tap do |new_invoice|
new_invoice.date = Date.today
new_invoice.number = nil <<<--------------
items.each do |item|
new_invoice.items.push item.dup
end
end
end
end
Now what I would like is to not copy the number attribute at all, so a new
number can be generated inside my new action (I am not showing that here
for brevity).
Right now, I am setting it to nil which is not what I want.
Any Ideas ?

No comments:

Post a Comment