uninitialized constant error in app but not when using console

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

Chris Hall wrote:

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, 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.

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

Chris Hall 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?

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.