0

I've just upgraded to Rails 7, and suddenly I'm having a very odd issue with one of my models.

This is the model (please ignore the terrible naming of the keys and the fact that this would probably be better-suited as a boolean – it's legacy code ):

class Product < ApplicationRecord

  FIREARM_NAMES = %w{ Frame Pistol Receiver Revolver Rifle Shotgun }
  PAGINATE_BY   = 24

  enum map_price_source: [:map_source_is_distributor, :map_source_is_brand]

  ...

end

And when I try to load the class in a Rails console (either by just calling Product or by instantiating it via Product.last), the following error message is thrown:

ArgumentError: You tried to define an enum named "map_price_source" on the model "Product", but this will generate a instance method "map_source_is_distributor?", which is already defined by another enum.

map_price_source is the only enum in the Product class so map_source_is_distributor can't be used by two different enums in Product. And, in fact, that enum value is unique across the entire application.

Furthermore, I'm getting some warnings in console as well:

.../app/models/product.rb:3: warning: already initialized constant Product::FIREARM_NAMES
.../app/models/product.rb:3: warning: previous definition of FIREARM_NAMES was here
.../app/models/product.rb:4: warning: already initialized constant Product::PAGINATE_BY
.../app/models/product.rb:4: warning: previous definition of PAGINATE_BY was here

The error and the warnings seem to indicate that the Product class is somehow being loaded more than once. And even more odd, as far as I can tell this is the only class that this is happening for.

If anyone has any clue what could be causing this, I'd be extremely grateful.

jeffdill2
  • 3,968
  • 2
  • 30
  • 47
  • are you doing something like `require 'product'` anywhere? one thing you can try is to put a debugger/byebug/binding.pry call at the beginning of the class and when it stops there you can check the stack to see if something looks off too – arieljuod Mar 17 '23 at 03:29

1 Answers1

0

It turned out to be a red herring.

There was a separate issue where we were using decorate_attribute_type, which was removed in Rails 7. Once I updated that functionality to no longer use decorate_attribute_type, the "double load" errors stopped occurring.

jeffdill2
  • 3,968
  • 2
  • 30
  • 47