RockPaperScissors

LizardSpock

Ruby Script Connect to DB Using database.yml

| Comments

To connect to a rails style DB config outside of the normal workflow, or using a database.yml in a different workflow:

require 'yaml'
require 'active_record'

environment = ENV['RACK_ENV'] || 'development'
dbconfig    = YAML.load(File.read('config/database.yml'))
ActiveRecord::Base.establish_connection dbconfig[environment]

#Load All models 
Dir.glob("./app/models/*").each do |file|
  require file 
end

#Now use Active record as you normally would
# @user = User.create( :name => 'Dave' )
# @user.save
# @first_person = User.first

An example database.yml

development:
    adapter: postgresql
    host: localhost
    port: 5432
    username: 
    password: 
    database: example_dev

Comments