Created
August 26, 2010 19:59
-
-
Save iirving/552127 to your computer and use it in GitHub Desktop.
as per Initialize max value in Ruby on Rails ActiveRecord, automagically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def after_initialize | |
if self.new_record? then | |
key = self.class.to_s | |
max_initialize_value_field = 'display_order' | |
$max_initialize_value = Hash.new() if !(defined? $max_initialize_value) | |
$max_initialize__value_dateTime = Hash.new() if !(defined? $max_initialize__value_dateTime) | |
if ( $max_initialize_value[key].nil? ) || ( ( !$max_initialize__value_dateTime[key].nil? )&& | |
( ( (Time.now - $max_initialize__value_dateTime[key]).to_i ) > ( 10 *60 ) )) then | |
max_initialize_value = self.class.maximum(max_initialize_value_field) | |
max_initialize_value = 0 if max_initialize_value.nil? | |
else | |
max_initialize_value = $max_initialize_value[key] | |
end | |
max_initialize_value = max_initialize_value + 1 | |
$max_initialize_value[key] = max_initialize_value | |
$max_initialize__value_dateTime[key] = Time.now | |
self.sequence_order = max_initialize_value | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
as per http://www.falsepositives.com/index.php/2010/08/23/initialize-max-value-in-ruby-on-rails-activerecord-automagically/ Initialize max value in Ruby on Rails ActiveRecord, automagically