Thursday, November 12, 2009

Manual active record db connection in ruby using mysql adapter

by Sandip Ransing 2 comments

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

blog comments powered by Disqus

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter