Here's how I'm using ActiveRecord outside of Rails:
#!/usr/bin/ruby
require 'active_record'
require 'mysql2' # or 'pg' or 'sqlite3'
ActiveRecord::Base.establish_connection(
adapter: 'mysql2', # or 'postgresql' or 'sqlite3'
database: 'DB_NAME',
username: 'DB_USER',
password: 'DB_PASS',
host: 'localhost'
)
# Note that the corresponding table is 'orders'
class Order < ActiveRecord::Base
end
Order.all.each do |o|
puts "o: #{o.inspect}"
end
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…