1

I am creating an application where a user logs into an Oracle Database and runs prepared queries. I would like to use ActiveRecord to help run the queries, but I am running into some issues.

When the user attempts to login, I test the connection by running

def test (username, password)
ActiveRecord::Base.establish_connection({
    :adapter => 'oracle_enhanced',
    :database => '//database:1521/test',
    :username => username,
    :password => password
})
end

However, if a user inputs incorrect login information, I get the following error:

OCIError

ORA-01017: invalid username/password; logon denied

on the rails page. When I try to reload the login page, it keeps showing that error and does not show the actual login page. The only way to remove this error is to restart the entire rails application.

Is this because I attempted to establish connection to a different database but it fails and it doesn't know what to do? Is there any way I can prevent this from happening?

josh
  • 9,656
  • 4
  • 34
  • 51
  • See a similar post on the matter: http://stackoverflow.com/questions/1226182/how-do-i-work-with-two-different-databases-in-rails-with-active-records – lebreeze May 02 '13 at 06:40
  • I cannot add the database information into `database.yml` because the user has to login with his/her own credentials and I can't establish a connection to the database before this. – josh May 02 '13 at 06:43

1 Answers1

2

I also got this issue before. To test a db connection, you should not use ActiveRecord::Base because it effects on the entire app, but you should create an abstract class instead. Here is the code:

class DbConnectionTest < ActiveRecord::Base
  @abstract_class = true
end

Then you just simply use the custom class:

DbConnectionTest.establish_connection({})
Blue Smith
  • 8,580
  • 2
  • 28
  • 33
  • That worked! I had to run `DbConnectionTest.establish_connection({}).checkout` to test, but it worked! I have an additional question: why does it have to be abstract? – josh May 02 '13 at 07:12
  • Abstract active record class means that it does not map to any table. If you don't set this property, it will raise an error when trying to use the class. Or you have to create and map a table to the class. – Blue Smith May 02 '13 at 07:21