Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iirving/546540 to your computer and use it in GitHub Desktop.
Save iirving/546540 to your computer and use it in GitHub Desktop.
as per Initialize max value in Ruby on Rails ActiveRecord, automagically
class Item < ActiveRecord::Base
validates_presence_of :display_order
validates_uniqueness_of :display_order
...
def after_initialize
if self.display_order.nil? then #if the section is new (created) and sequence order has no value
if $item_display_order_max_value.nil? then
$item_display_order_max_value = Item.maximum('display_order')
$item_display_order_max_value = 0 if item_display_order_max_value.nil?
$item_display_order_max_value += 1
self.display_order = $item_display_order_max_value
else
$item_display_order_max_value += 1
self.display_order = $item_display_order_max_value
end
end
@iirving
Copy link
Author

iirving commented Aug 24, 2010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment