no implicit conversion of Symbol into Integer when

2019-09-08 07:51发布

问题:

So I'm working on an existing app and for some reason I was able to get it working and mirrored on my local environment however when I try migrating on heroku I get the below error, any ideas?

Error:

== 20141119113015 CreateReleasedInventoryStatus: migrating ====================
-- create_enum("eh_released_inventory_status", ["rejected", "pending", "allocated", "released", "transferred"])
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

no implicit conversion of Symbol into Integer/app/vendor/bundle/ruby/2.0.0/gems/power_enum-2.8.0/lib/power_enum/schema/schema_statements.rb:84:in `[]'
/app/vendor/bundle/ruby/2.0.0/gems/power_enum-2.8.0/lib/power_enum/schema/schema_statements.rb:84:in `create_enum'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:662:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:652:in `method_missing'
/app/db/migrate/20141119113015_create_released_inventory_status.rb:3:in `change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:606:in `exec_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:589:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection

Migration file:

class CreateReleasedInventoryStatus < ActiveRecord::Migration

  def change
    create_enum("eh_released_inventory_status", ["rejected", "pending", "allocated", "released", "transferred"])
  end
end

I'm also using the following gem:

gem 'power_enum'

回答1:

Your migration syntax seems to be incorrect for the power_enum gem that you're using.

The gem's README says the syntax uses a name, then an options hash, such as:

create_enum("status", {…})

Whereas you're using a name, then an array:

create_enum("eh_released_inventory_status", […])

As far as I can tell, the solution is to change your migration line from what you posted to the power_enum syntax. The README has a bunch of examples, and also looks like it's thorough and very well documented.



回答2:

I think you migration should look like this

class CreateReleasedInventoryStatus < ActiveRecord::Migration

  def change
    create_enum :released_inventory_status, name_column: :status, description: true, name_limit: 5
    ["rejected", "pending", "allocated", "released", "transferred"].each do |status| 
      ReleasedInventoryStatus.update_enumerations_model do |klass|
         #0.8.1 < version < 0.9.3 does not yield the klass 
         #in this case use: 
         #ReleasedInventoryStatus.create
         #in place of klass.create
         klass.create :name        => status,
                      :description => status.capitalize
      end
    end
  end
end

This will create the table released_inventory_statuses with status and description columns with a limit of 5 unique statuses. Then it will populate status and description for each item in you list through update_enumeration_model. This should work for your use case but I have not tested this theory. My answer is purely based on how create_enum works and then this section of the README

update_enumerations_model (since version 0.8.1)

The preferred mechanism to update an enumerations model in migrations and similar. Pass in a block to this method to to perform any updates.