# Uninitialized constant error in app but not when using conso

**URL:** <https://www.ruby-forum.com/t/uninitialized-constant-error-in-app-but-not-when-using-conso/77905>\
**Category:** Rails\
**Created:** [December 1, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error-in-app-but-not-when-using-conso/77905 "2006-12-01T13:31:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [December 1, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error-in-app-but-not-when-using-conso/77905/1 "2006-12-01T13:31:43Z")

</div>

I’ve got a wierd issue here and maybe someone can assist

i created a module in my apps lib directory which i use for  
authenticating against an external source (among other things).

## lib/myapp.rb

require ‘rubygems’  
require\_gem ‘activerecord’

# uses activerecord odbc adapter by openlink

module MyApp  
module ExternalSource  
class Connection \< ActiveRecord::Base  
def self.authenticate(opts = {:login =\> “”, :password =\> “”, :env  
=\> “”)  
begin  
ActiveRecord::Base.establish\_connection(  
:adapter =\> “odbc”,  
:dsn =\> opts[:env],  
:username =\> opts[:login],  
:password =\> opts[:password],  
:trace =\> true  
)  
rescue Exception =\> e  
raise e  
ensure  
self.connection.disconnect! if self.connection.active?  
end  
true  
end  
end  
end  
end

in my auth controller i have

## auth\_controller.rb

require ‘myapp.rb’

class AuthController \< ApplicationController  
def login  
begin  
auth = MyApp::ExternalSource::Connection.authenticate(:login =\>  
params[:login], :password =\> params[:password], :env =\> “MYAPP\_” +  
RAILS\_ENV.upcase)  
rescue Exception =\> e  
…  
end  
end

def logout  
…  
end  
end

now, when i attempt to login, i get an exception raised:

uninitialized constant Connection

however, if i run it from the console:

Loading development environment.

> > require ‘MyApp’  
> > =\> true
> 
> > auth = MyApp::ExternalSource::Connection.authenticate(:login =\>  
> > “xxx”, :password =\> “xxx”, :env =\> “xxx”)  
> > =\> true

no constant error.

any ideas on this?

Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [December 1, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error-in-app-but-not-when-using-conso/77905/2 "2006-12-01T16:56:30Z")

</div>

Chris H. wrote:

> ```
> :password => opts[:password],
> 
> ```
> 
> end  
> def login  
> …
> 
> > > require ‘MyApp’  
> > > =\> true
> > 
> > > auth = MyApp::ExternalSource::Connection.authenticate(:login =\>  
> > > “xxx”, :password =\> “xxx”, :env =\> “xxx”)  
> > > =\> true
> 
> no constant error.
> 
> any ideas on this?

Chris, are you sure the myapp.rb file you’re requiring in  
your controller and the MyApp.rb file you’re requiring in  
your console test are the same, because there’s a missing  
close brace in the parameters to authenticate in the source  
of myapp.rb you posted.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [December 1, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error-in-app-but-not-when-using-conso/77905/3 "2006-12-01T17:18:36Z")

</div>

Mark,

that’s a typo and cut/paste error on my part.

module MyApp  
module ExternalSource  
class Connection \< ActiveRecord::Base  
def self.authenticate(opts = {:login =\> “”, :password =\> “”, :env  
=\> “”})  
begin  
self.establish\_connection(  
:adapter =\> “odbc”,  
:dsn =\> opts[:env],  
:username =\> opts[:login],  
:password =\> opts[:password],  
:trace =\> true  
)  
rescue Exception =\> e  
raise e  
ensure  
self.connection.disconnect! if self.connection.active?  
end  
true  
end  
end  
end  
end

additional info:

if i add the following in my login action

logger.info MyApp.constants.join(", ")

i see (in log):

ExternalSource

if i add:

logger.info MyApp::ExternalSource.constants.join(", ")

i get a blank line in the log

from the console i see:

> > MyApp.constants  
> > =\> [“ExternalSource”]
> 
> > MyApp::ExternalSource.constants  
> > =\> [“Connection”]

Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [December 1, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error-in-app-but-not-when-using-conso/77905/4 "2006-12-01T19:57:18Z")

</div>

Is your app in production mode?

no.

Have you got more than one version of the ActiveRecord gem or vendor  
dist installed?

no, only AR 1.14.4

Have you tried requiring myapp.rb in environment.rb?

yes, exhibits same behavior.

i’ve done some more testing and have concluded that it has something  
to do with the Connection class being a subclass of ActiveRecord::Base  
and that it’s used within a rails app.

allowing Rails to display the exception in the browser shows me:

This error occured while loading the following files:  
my\_app/external\_source/connection.rb

which tells me rails is mapping MyApp::ExternalSource::Connection to  
the above path which of course does not exist.

if i remove making Connection a subclass of ActiveRecord::Base, the  
problem I have goes away, however, the AR connection I make in  
Connection becomes the default Rails connection and my app breaks  
entirely.

Specifically, I just need a connection to the external source for:

1. authentication (done by creating a connection to the external  
source. if connection established, auth is successful).
2. running various queries through the connection as stored procedure  
calls.

what I don’t need is all the automagic extras. just the connection  
please.

fyi, using the module alone works great. it’s only when i attempt to  
use it within rails that i have problems.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [December 1, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error-in-app-but-not-when-using-conso/77905/5 "2006-12-01T17:58:54Z")

</div>

Chris H. wrote:

> logger.info MyApp::ExternalSource.constants.join(", ")
> 
> i get a blank line in the log

Do you see any errors in the log regarding the loading  
of the MyApp module?

Is your app in production mode? Have you got more than  
one version of the ActiveRecord gem or vendor dist installed?  
Have you tried requiring myapp.rb in environment.rb?

–  
We develop, watch us RoR, in numbers too big to ignore.
