Generate sequential numbers from database


I'm using single table inheritance to handle customer baskets, sales
orders, sales invoices etc...

Due to this I don't really want to use the table id as the order
reference, invoice number etc as the baskets would likely send the
numbers rocketing into unnecessarily large figures, with potentially
large number gaps between sequential orders.

That is one order might have order number 1000, and the next confirmed
order might end up with 1030.

I need to be able to generate a next sequential number once the order
is confirmed and this becomes its order number. In the past I might
have a table with one row that contains an integer that is incremented
for each confirmed order. How would this best be tackled via

I've thought of creating a model for ConfirmedOrder that just
contained a number field but it seemed a little wasteful and creates
an unnecessary association.

Maybe the STI is causing me headaches but it seemed eaiser given that
these documents all have such similar contents. Any thoughts?

Thanks, Andrew.

Add an OrderNumber column to the main model. Wherever in your logic
you're converting the record into a confirmed order, you should be
able to do something like

this_order.order_number = MainModel.maximum(:order_number) + 1

Thanks, that's a really concise solution.

A bit of transactional logic to avoid concurrency issues and I think
that will do!