ActiveRecord 是迄今为止我所见到的最好的ORM library ,除了DB2,支持所有的主流数据库。如果你想知道如何单独使用Activerecord,please follow me。
介绍一下我的系统环境:
OS:windows2003
DataBase: Oracle10g
Ruby: 1.8.7 (2008-05-31 patchlevel 0) [i386-mswin32]
activerecord: 2.2.2
activerecord-oracle_enhanced-adapter: 1.1.8
ruby-oci8:1.0.3
准备工作:
1.安装ruby,不用说了吧
2.安装activerecord, gem install activerecord
3.安装adapter, gem install activerecord-oracle_enhanced-adapter
4.创建一个试验目录 mkdir dbtest
ok,that's all
创建一个rb文件,内容如下
- require 'rubygems'
- require 'active_record'
- ActiveRecord::Base.establish_connection(
- :adapter => 'oracle_enhanced',
- :database => 'test', #oracle service name
- :username => 'system',
- :password => 'system')
你还可以把数据库的配置信息单独放在一个文件中,创建一个文件database.yml,内容如下
- adapter: oracle_enhanced
- database: test
- username: system
- password: system
更改rb文件为
- require 'rubygems'
- require 'active_record'
- require 'yaml'
- dbconfig = YAML::load(File.open('database.yml'))
- ActiveRecord::Base.establish_connection(dbconfig)
试一试效果
- require 'rubygems'
- require 'active_record'
- require 'yaml'
- dbconfig = YAML::load(File.open('database.yml'))
- ActiveRecord::Base.establish_connection(dbconfig)
- class User < ActiveRecord::Base
- set_table_name "my_user"
- end
- puts User.count
如果你想给activerecord加上日志,可以这么做
- require 'rubygems'
- require 'active_record'
- require 'yaml'
- require 'logger'
- dbconfig = YAML::load(File.open('database.yml'))
- ActiveRecord::Base.establish_connection(dbconfig)
- ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'a'))
- class User < ActiveRecord::Base
- set_table_name "my_user"
- end
- puts User.count
这样就产生了一个database.log文件
OK,今天就到这里吧