Skip to content

Instantly share code, notes, and snippets.

@jimmybaker
Created June 15, 2009 19:08
Show Gist options
  • Save jimmybaker/130274 to your computer and use it in GitHub Desktop.
Save jimmybaker/130274 to your computer and use it in GitHub Desktop.
class NextNumber < ActiveRecord::Base
class << self
VALID_CLASSES = [PurchaseOrder, SalesOrder]
def get_next_number_for(object)
throw Exception.new("Unsaved object not allowed") if object.new_record?
throw Exception.new("Invalid object type for NextNumber#get_next_number_for") unless VALID_CLASSES.include?(object.class)
begin
next_number = NextNumber.find_by_sequential_type(object.class.to_s)
next_number.sequential_id = object.id
next_number.save
next_number.lock_version
rescue ActiveRecord::StaleObjectError => e
retry
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment