The correct way to connect rails to an external database is using the config/database.yml file:
# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
defaults: &defaults
  adapter: postgresql
  encoding: utf8
  template: template0
# used for test & development
local:
  host: localhost
  username: j_random_user # change this!
  password: p4ssword # change this!
development:
  <<: *defaults
  <<: *local
  database: my_app_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *defaults
  <<: *local
  database: my_app_test
production:
  <<: *defaults
  host: "my_app_something.rds.amazonaws.com" # change this!
  username: my_amazon_db_user # change this!
  password: my_amazon_user # change this!
  reconnect: true
  port: 3306
You may want to use a local postgres database for development and mirror the production database with pgbackups.
But your main issue is that your are doing pretty much everything wrong when it comes to creating a rails application. That looks like a PHP example where some clueless soul is reinventing an database manager for the 1000th time. 
So here is a fast Rails MVC & ActiveRecord crash course:
Models reflect the objects in your domain. Lets say we have a pet store app.
We of course need a Pet model:
$ rails g model Pet name:string
$ rake db:migrate
This creates a pets table in the database and a Pet class. Note that the table name is plural and the model name is singular. 
# app/models/pet.rb
class Pet < ActiveRecord::Base
end 
We can then access the pets by:
Pet.all
Pet.find(1) # find pet where id is 1
# ... etc
And we can create pets by:
pet = Pet.create(name: "Spot")
All of this is covered in most basic rails tutorials.
Connecting without a model.
ActiveRecord::Base.establish_connection
con = ActiveRecord::Base.connection
res = con.execute('SELECT * FROM foo')
Although using ActiveRecord does not really make sense if you are not actually using a model per table and following the MVC conventions somewhat. It's possible, but it gives you no real benefits at all.
Likewise doing Rails without MVC is doable but what's the point?
Using a legacy database
let's say you have a legacy database written by someone who though using Apps Hungarian for database columns (shrug) was cool:
persons:
  intPersonID: Integer, Autoincrement, Primary Key 
And you want to map this to a Rails model
 class User < ActiveRecord::Base
   self.table_name 'persons'
   self.primary_key 'intPersonID'
 end
Or in your case:
class Test < ActiveRecord::Base
  self.table_name 'test' # since it's not "tests"
end
Test.where(city: "New York")
     
    
Pgdb? Rails models are objects which are supposed to reflect the domain objects in your application (like User, Car, Company..) not the database connection itself.