Class and module with the same name - how to choose one or the other?

I came across the following situation:

Exists

ModuleA::ModuleB::ClassC.do_something 

in the definition of do_something I need to use the model from the application

 def do_something ... data = Order.all ... end 

But there is also a module

 ModuleA::Order 

So, I get an error message

 undefined method `all' for ModuleA::Order:Module 

I found a solution by doing

 def do_something ... data = Kernel.const_get('Order').all ... end 

This returns the model. My question is: what is the best way to do this? is there a cleaner solution? (despite the fact that the same name for the class and module is not the biggest idea, but it cannot be changed here ...)

+8
ruby module class ruby-on-rails
source share
1 answer

Class name prefix :: in do_something method ...

 def do_something ... data = ::Order.all ... end 
+19
source share

All Articles