brainspec / enumerize

Enumerated attributes with I18n and ActiveRecord/Mongoid support
MIT License
1.72k stars 190 forks source link

undefined method `empty?' for 0:Integer #437

Closed GiuseppeVarriale closed 7 months ago

GiuseppeVarriale commented 8 months ago

When setting up a project, a migration is run that creates an object. Later in the model, an enumerize attribute with a default value is added for a column that does not yet exist (it will be created in a future migration). This sequence of events raises the following error:

StandardError: An error has occurred, this and all later migrations canceled:

undefined method `empty?' for 0:Integer
/home/pepe/projects/maino/comexnfe/db/migrate/20221101175500_create_plano_base_quantico.rb:3:in `up'
bin/rails:4:in `<main>'

Caused by:
NoMethodError: undefined method `empty?' for 0:Integer
/home/pepe/projects/maino/comexnfe/db/migrate/20221101175500_create_plano_base_quantico.rb:3:in `up'

model line:

enumerize :tipo_de_menu, in: { menu_padrao: 1, icones_dashboard: 2 }, i18n_scope: "planos_base.tipo_de_menu", scope: true, default: :menu_padrao

This issue occurs when the tipo_de_menu column does not exist at the time the enumerize attribute is defined.

steps to reproduce

1 -create a model with some columns, the migration, etc.

rails generate model Person name:string age:integer code: int

2 - create another migration that create an object and persisting in the database

class CreatePersonWithCode20 < ActiveRecord::Migration[5.2]
  def up
    Person.create!(name: 'John Doe', age: 18, code: 20 )
  end

  def down
      Person.find_by(code: 20).destroy
  end
end

3 - create the migration by creating the column that will store integer value

class AddTypeToPerson < ActiveRecord::Migration[5.2]
  def change
    add_column :person, :type, :integer
  end

end 4 - add enumerize to the model with the custom values format and default value

   #person model
   #...
   enumerize :type, in: { standard_type: 0, advanced_type: 1 }, i18n_scope: "planos_base", scope: true, default: 
   :standard_type

5- run the migrations

   rails db:migrate
nashby commented 8 months ago

@GiuseppeVarriale hey, I couldn't reproduce it locally. Could you please provide a simple application that reproduces it?

GiuseppeVarriale commented 8 months ago

https://github.com/GiuseppeVarriale/enumerize_error/

image

nashby commented 8 months ago

@GiuseppeVarriale thank you! Looks like you're on pretty old Rails that is not supported anymore. If I try to upgrade to 6.1 in your example app then it works. If you still need to use that old version Rails and Enumerize to fix that issue you can just comment out enumerize call and uncomment it afterwards.