Below is ruby code to establish manual database connection.
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "root",
:password => "abcd",
:database => "funonrails"
)
Snippet of database.yml file
common: &common
adapter: mysql
database: students_dev
username: root
password:
host: localhost
students_development:
<<: *common
database: students_dev
students_production:
<<: *common
database: students
Load database configurations from yml file
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection( dbconfig[:students_development] )
Active Record Model & DB connection
class Student < ActiveRecord::Base
establish_connection "students_production"
set_table_name "student"
set_primary_key "stud_number"
end