| « 我家小宝的照片 | Build kernel for Ubuntu 10.04 » |
ActiveRecord without Rails
Link: http://blog.aizatto.com/2007/05/21/activerecord-without-rails/
The basic idea is to using following method to connect database.
require "active_record"
ActiveRecord::Base.establish_connection(params)
The params must be a hash or object.
Based on this method, user can use yaml to load configure just like rails do:
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)
SQLite sample
development: adapter: sqlite3 database: db/development.db pool: 5 timeout: 5000
Migration
ActiveRecord::Migrator.up('db/migrate')
class CreateLogTables < ActiveRecord::Migration
def self.up
create_table :accounts do |t|
t.integer :account_id
end
end
end
Using ActiveRecord
class Account < ActiveRecord::Base
has_many :sessions
end
class Session < ActiveRecord::Base
belongs_to :user
has_many :actions
end
class Action < ActiveRecord::Base
belongs_to :session
end
Logger
ActiveRecord::Base.logger = Logger.new(File.open('log/database.log', 'a'))
ActiveRecord::Base.colorize_logging = false