Created
August 23, 2010 20:54
-
-
Save iirving/546315 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
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 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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