DoeJ DoeJ - 1 year ago 78
Ruby Question

How to start a number column from a specific number?

I need to generate a unique incrementing number that will be part of a more complex invoice number. I would like to keep the increment logic in the app.

I must resume the numbering that is currently going on another system.

I intend to do


But how can I start the
column from a given number?

Answer Source

If I understand your question, you just need to reset the index of the primary key of your table (Model) to the number that you want.

If your are using MySQL or PostgreSQL then:

ALTER SEQUENCE tablename_id_seq RESTART WITH 1453;

then the next record will have for index in this case the 1453

For access the DB console just run on your console rails db


before_filter :check_invoice_number 
def check_invoice_number
  self.invoice_column = Model.last.invoice_column + 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download