jeudi 23 juillet 2015

Rails delete table row via migration

I'm trying to delete several rows in the table actionable_items via the following migration. I have debugged and can confirm that the variables that store the table row are not nil. The migration runs successfully, but it doesn't delete the row from the table. Also, does anyone know why I can debug a migration when I run rake db:migrate:redo but not when I run 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

I also tried deleting from the rails console, but once again, pgAdmin is showing the row not deleted.

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">

Aucun commentaire:

Enregistrer un commentaire