Skip to content

Instantly share code, notes, and snippets.

@mleszcz
Created July 13, 2011 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mleszcz/1081491 to your computer and use it in GitHub Desktop.
Save mleszcz/1081491 to your computer and use it in GitHub Desktop.
module OrderedModel
def up
self.class.update_all 'ord = ord + 1', "ord = #{self.ord-1}"
update_attribute :ord, self.ord-1
end
def down
self.class.update_all 'ord = ord - 1', "ord = #{self.ord+1}"
update_attribute :ord, self.ord+1
end
def set_ord
self.class.update_all 'ord = ord - 1', "ord > #{self.ord}"
end
end
class Gallery < ActiveRecord::Base
include OrderedModel
has_many :photos
before_destroy :set_ord
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment