Rails 通过迁移删除 table 行

Rails delete table row via migration

我正在尝试通过以下迁移删除 table actionable_items 中的几行。我已经调试并且可以确认存储 table 行的变量不为零。迁移 运行 成功,但不会从 table 中删除行。另外,有谁知道为什么我可以在 运行 rake db:migrate:redo 时调试迁移,但在 运行 rake db:migrate 时却不能?

class RemoveActionableItems < ActiveRecord::Migration
  class ActionableItem < ActiveRecord::Base
    attr_accessible :actionable_item, :name, :sequence, :type
  end

  class MenuItemTEMP < ActionableItem
    self.table_name = "actionable_items"
  end

  class InsightReportMenuItemTEMP < ActionableItem
    self.table_name = "actionable_items"
  end

  def up
    validation_settings = MenuItem.find_by_name("Validation Settings")
    identifier_lookup = MenuItem.find_by_name("Identifier Lookup")
    compliance = InsightReportMenuItem.find_by_name("Compliance")
    debugger
    validation_settings.destroy! #unless validation_settings.nil?
    identifier_lookup.destroy! #unless identifier_lookup.nil?
    compliance.destroy! #unless compliance.nil?
  end

  def down
    MenuItem.create :name => "Validation Settings", :type => "MenuItem"
    MenuItem.create :name => "Identifier Lookup", :type => "MenuItem"
    InsightReportMenuItem.create :name => "Compliance", :type => "InsightReportMenuItem"
  end
end

我也尝试从 rails 控制台删除,但 pgAdmin 再次显示未删除的行。

pmpaware-webapp(development)> compliance = InsightReportMenuItem.find_by_name("Compliance")
  InsightReportMenuItem Load (3.8ms)  SELECT "actionable_items".* FROM "actionable_items" WHERE "actionable_items"."type" IN ('InsightReportMenuItem') AND "actionable_items"."name" = 'Compliance' LIMIT 1
=> #<InsightReportMenuItem id: 264, name: "Compliance", actionable_item_id: nil, created_at: "2015-07-23 18:57:25", updated_at: "2015-07-23 18:57:25", actionable_items_count: 0, sequence: nil, type: "InsightReportMenuItem">
pmpaware-webapp(development)> compliance.errors
=> #<ActiveModel::Errors:0x007fc0735ac540 @base=#<InsightReportMenuItem id: 264, name: "Compliance", actionable_item_id: nil, created_at: "2015-07-23 18:57:25", updated_at: "2015-07-23 18:57:25", actionable_items_count: 0, sequence: nil, type: "InsightReportMenuItem">, @messages={}>
pmpaware-webapp(development)> compliance.delete

  SQL (111829.8ms)  DELETE FROM "actionable_items" WHERE "actionable_items"."type" IN ('InsightReportMenuItem') AND "actionable_items"."id" = 264
=> #<InsightReportMenuItem id: 264, name: "Compliance", actionable_item_id: nil, created_at: "2015-07-23 18:57:25", updated_at: "2015-07-23 18:57:25", actionable_items_count: 0, sequence: nil, type: "InsightReportMenuItem">

找到解决方案

class RemoveActionableItems < ActiveRecord::Migration
  class ActionableItem < ActiveRecord::Base
    attr_accessible :actionable_item, :name, :sequence, :type
  end

  class MenuItemTEMP < ActionableItem
    self.table_name = "actionable_items"
  end

  class InsightReportMenuItemTEMP < ActionableItem
    self.table_name = "actionable_items"
  end

  def up
    MenuItem.delete_all(name: "Validation Settings") unless MenuItem.find_by_name("Validation Settings").nil?
    MenuItem.delete_all(name: "Identifier Lookup")  unless MenuItem.find_by_name("Identifier Lookup").nil?
    InsightReportMenuItem.delete_all(name: "Compliance")  unless InsightReportMenuItem.find_by_name("Compliance").nil?
  end

  def down
    MenuItem.create :name => "Validation Settings", :type => "MenuItem"
    MenuItem.create :name => "Identifier Lookup", :type => "MenuItem"
    InsightReportMenuItem.create :name => "Compliance", :type => "InsightReportMenuItem"
  end
end