change column type to integer rails

Viewed 4605

I have run a migration when trying to change on heroku table column from string to integer: This is my migration:

class ChangePriceTypeInItems < ActiveRecord::Migration
  def change
    change_column :items, :price, :integer
  end
end

And this is my error: What do I do?

ActiveRecord::StatementInvalid: PG::Error: ERROR:  column "price" cannot be cast automatically to type integer
HINT:  You might need to specify "USING price::integer".
: ALTER TABLE "items" ALTER COLUMN "price" TYPE integer
4 Answers
class ChangePriceTypeInItems < ActiveRecord::Migration
  safety_assured

  def change
    # Take the back-up of your data first
    Item.update_all(price: nil)
    change_column :items, :price, :integer, using: 'price::integer'
  end
end
Related