Search code examples
ruby-on-rails-3controllermodeactiveresourceuninitialized-constant

Ruby on rails: model name uninitialized error (mismatch of module)


I am accessing model Company::Item in controller Security::MyController. It is giving error uninitialised constant Security::Company::Item . So basically it is appending 'Security::' for given model. It is not the case with some other models say Security::User(model in same module security). What could be possible explanation for this?


Solution

  • This is a scope resolution problem. You should try using ::Company::Iteminside Security::MyController

    According to Ruby Language Spec

    ::Something is a shortcut for Object::Something. The idea is that ::Something 
    should look up a constant called Something in the global scope, but since ruby 
    doesn't truly have a global scope, it looks it up in the Object class, which is 
    the nearest thing in ruby to a global scope.
    

    Prefixing :: will prevent Ruby from applying the default scope in that context, which in your case, is the Security:: scope