Hi, 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 ActiveRecord? 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
MikeG1-F66HEGuBJ/9Wk0Htik3J/w@public.gmane.org
2008-Aug-28 13:42 UTC
Re: Generate sequential numbers from database
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, that''s a really concise solution. A bit of transactional logic to avoid concurrency issues and I think that will do! On Aug 28, 2:42 pm, "Mik...-F66HEGuBJ/9Wk0Htik3J/w@public.gmane.org" <Larkw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---