Skip to content

Instantly share code, notes, and snippets.

@iirving
Created August 23, 2010 20:54
Show Gist options
  • Save iirving/546315 to your computer and use it in GitHub Desktop.
Save iirving/546315 to your computer and use it in GitHub Desktop.
as per Initialize max value in Ruby on Rails ActiveRecord, automagically
class ItemController < ApplicationController
....
# GET /items/new
# GET /items/new.xml
def new
@item= Item.new
@item.display_order = get_new_item_display_order
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @item}
end
end
...
private
...
def get_new_item_display_order
max_value = Item.maximum('display_order')
max_value = 0 if max_value.nil?
max_value += 1
end
@iirving
Copy link
Author

iirving commented Aug 23, 2010

as explained in 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

@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