Account numbering in Rails + MySQL

I would like invoices to be organized in this format:

AB20140001

      

AB

is an acronym for our company, 20140001

this is the first invoice in 2014. The current quick idea is to add to the table the invoices

columns year

and invoice_number

and where the 2014

current number of invoices ( 0001

for the first) will be stored .

However, how to store the column invoice_number

in the format XXXX

(there will be a number for the second invoice 0002

, etc.)?

Or - is there a better way to streamline the numbering of accounts?

Thanks in advance.

+3


source to share


1 answer


I would implement a function that runs after creation and sets the serial number

We used :after_create

so that we can use :id

after installing it.



class Invoice < ActiveRecord::Base
  after_create :set_invoice_numeber

  def set_invoice_number
    self.update_attribute(:invoice_number, 'AB' + Time.now.year + '%.4d' % id)
  end

end

      

+2


source







All Articles