santuxus
santuxus

Reputation: 3702

Class and Module with the same name - how to choose one or another?

I have encountered following situation:

There is

ModuleA::ModuleB::ClassC.do_something

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

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

But there also exists a module

ModuleA::Order

So I get an error

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

I found a solution by doing

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

That returns the model. My question is: what's the best way to do it? is there a cleaner solution? (despite the fact, that having the same name for Class and Module it's not the greatest idea, but it cannot be changed here...)

Upvotes: 9

Views: 4379

Answers (1)

idlefingers
idlefingers

Reputation: 32067

Prefix the class name with :: in the do_something method...

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

Upvotes: 20

Related Questions