I'm haviing an issue adding a constant to a model ...
# support
l
I'm haviing an issue adding a constant to a model though a decorator, just testes in the Rails console and seems is not loading it:
Copy code
2.6.6 (console)[3] » Spree::Product::SEARCHER_STRING_LIMIT
NameError: uninitialized constant Spree::Product::SEARCHER_STRING_LIMIT
am I missing something else? Thanks.
j
I can't tell why your code isn't working if you don't show the code. 😅
My guess is you need to use include/prepend rather than class_eval, but I'd need to see your decorator to know for sure.
constants (and constant lookup) in Ruby work very differently than many other languages.
class_eval
is changes the current class and sets
self
to it, but doesn't affect the scope as far as constants are concerned
l
yes, I'm using
class_eval
Copy code
Spree::Product.class_eval do
  SEARCHER_STRING_LIMIT = 160
  ...
end
j
For starters: using class eval is discouraged. You should use include or prepend. It will work if you switch to that. Alternatively, set the constant like
Spree::Product::SEARCHER_STRING_LIMIT = 160
l
thanks @Jared Norman 🤙
sg horns 1