Rails 4: How to "Duplicate" or Change State of Records from model 'A' to model 'B'?

63 Views Asked by At

I have model Quote and model Invoice. The database for each of these models have the columns: date, company, product and price. When a customer approves a quote I would like to turn that quote into a invoice with the same values but with the current date and its own invoice_id.

What code should I include in my Invoice model so that the record "duplicates" or "changes" state?

Thank you

1

There are 1 best solutions below

4
On BEST ANSWER

In Quote you can use the following code

after_save :generate_invoice, :if => :approved?

def approved?
 # your code to return true or false, this method should return true only one time, handle it carefully.
end

def generate_invoice
    Invoice.create!(date: Time.now, company: self.company, product: self.product, price: self.price)
end