072_add_enumerations_position.rb 494 Bytes
Newer Older
jplang's avatar
jplang committed
1
2
class AddEnumerationsPosition < ActiveRecord::Migration
  def self.up
3
    add_column(:enumerations, :position, :integer, :default => 1) unless Enumeration.column_names.include?('position')
jplang's avatar
jplang committed
4
    Enumeration.all.group_by(&:opt).each do |opt, enums|
5
6
      enums.each_with_index do |enum, i|
        # do not call model callbacks
7
        Enumeration.where({:id => enum.id}).update_all(:position => (i+1))
8
      end
jplang's avatar
jplang committed
9
10
11
12
13
14
15
    end
  end

  def self.down
    remove_column :enumerations, :position
  end
end