Rails通过迁移删除表行

时间:2015-07-23 19:19:44

标签: ruby-on-rails ruby-on-rails-3 postgresql migration delete-row

我尝试通过以下迁移删除表actionable_items中的多行。我已经调试并且可以确认存储表行的变量不是nil。迁移成功运行,但它不会从表中删除该行。另外,有没有人知道为什么我可以在运行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">

1 个答案:

答案 0 :(得分:1)

找到解决方案

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