Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created October 17, 2012 14:43
Show Gist options
  • Save yao2030/3905894 to your computer and use it in GitHub Desktop.
Save yao2030/3905894 to your computer and use it in GitHub Desktop.
using ActiveRecord outside rails
require 'rubygems'
require 'active_record'
require 'mysql2'
require 'yaml'
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)
class Duck < ActiveRecord::Base
end
puts Duck.count
adapter: mysql
database: dbfile
username: username
password: 123
host: localhost
gem install mysql2
add require 'mysql2' to ar_example.rb
create a table 'ducks' in database 'dbfile'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment