Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a module and class with the same name

I have a module stat that exists in the directory structure: lib/stat_creator/stat/

In lib/stat_creator/stat.rb I have the files in the lib/stat_creator/stat/ directory that I require, as well as:

module StatCreator
    module Stat
    end
end

When I use that module I refer to the classes as

StatCreator::Stat::Foo.new 

Now I want a root Stat class that lives in app. I've made my Stat class in app/models and set it up in routes.rb. But if I go to rails console and try to use the Stat class in app/models like:

Stat.by_user_id("ID")

I get the error: LoadError: Expected ../lib/stat_creator/stat.rb to define Stat

I thought the point of using namespaces was to avoid this kind of conflict, so I don't understand what I"m doing wrong.

like image 753
Jeremy Smith Avatar asked Jun 05 '11 19:06

Jeremy Smith


1 Answers

I'd do:

::Stat.by_user_id("ID")
like image 155
apneadiving Avatar answered Sep 22 '22 12:09

apneadiving