diff --git a/db/migrate/20110520150544_remove_categories_with_invalid_type.rb b/db/migrate/20110520150544_remove_categories_with_invalid_type.rb index 53f44d7..214b6e2 100644 --- a/db/migrate/20110520150544_remove_categories_with_invalid_type.rb +++ b/db/migrate/20110520150544_remove_categories_with_invalid_type.rb @@ -2,14 +2,17 @@ class RemoveCategoriesWithInvalidType < ActiveRecord::Migration def self.remove_invalid(category) if category.class != ProductCategory && !category.class.ancestors.include?(ProductCategory) - category.destroy + execute("update categories set type='ProductCategory' where id=#{category.id}") else category.children.map { |child| remove_invalid(child) } end end def self.up - ProductCategory.all.map { |category| remove_invalid(category)} + select_all("SELECT id from categories WHERE type = 'ProductCategory'").each do |product_category| + category = ProductCategory.find(product_category['id']) + remove_invalid(category) + end end def self.down diff --git a/db/schema.rb b/db/schema.rb index 3ee7061..3ac08bd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110316171323) do +ActiveRecord::Schema.define(:version => 20110520150544) do create_table "action_tracker", :force => true do |t| t.integer "user_id" diff --git a/test/functional/categories_controller_test.rb b/test/functional/categories_controller_test.rb index e11112e..9f1121d 100644 --- a/test/functional/categories_controller_test.rb +++ b/test/functional/categories_controller_test.rb @@ -170,7 +170,7 @@ class CategoriesControllerTest < Test::Unit::TestCase should 'use parent\'s type to determine subcategory\'s type' do parent = ProductCategory.create!(:name => 'Sample category', :environment => Environment.default) post :new, :parent_id => parent.id, :parent_type => parent.class.name, :category => {:name => 'Subcategory'} - sub = Category.last + sub = ProductCategory.find_by_name('Subcategory') assert_equal parent.class, sub.class end -- libgit2 0.21.2