Use Activerecord from Rails

Source: Internet
Author: User

ActiveRecord is the best ORM library I have ever seen. Apart from DB2, ActiveRecord supports all mainstream databases. If you want to know how to use Activerecord and please follow me separately.

 

Introduce my system environment:

OS: windows2003

DataBase: Oracle10g

Ruby: 1.8.7 (patchlevel 0) [i386-mswin32]

Activerecord: 2.2.2

Activerecord-oracle_enhanced-adapter: 1.1.8.

Ruby-oci8: 1.0.3.

 

Preparations:

1. Install ruby. Needless to say

2. install activerecord and gem install activerecord

3. install adapter, gem install activerecord-oracle_enhanced-adapter

4. Create a test directory mkdir dbtest

OK, that's all

 

Create a rb file with the following content:

 

Ruby code
  1. Require 'rubygems'
  2. Require 'Active _ record'
  3. ActiveRecord: Base. establish_connection (
  4. : Adapter => 'oracle _ enabled ',
  5. : Database => 'test', # oracle service name
  6. : Username => 'system ',
  7. : Password => 'system ')
require 'rubygems'require 'active_record'ActiveRecord::Base.establish_connection(:adapter  => 'oracle_enhanced',:database => 'test',   #oracle service name:username => 'system',:password => 'system')  

 

 

You can also separately store the database configuration information in a file to create a file database. yml with the following content:

 

Ruby code
  1. Adapter: oracle_enhanced
  2. Database: test
  3. Username: system
  4. Password: system
adapter:  oracle_enhanceddatabase: testusername: systempassword: system

 

Change the rb file

 

Ruby code
  1. Require 'rubygems'
  2. Require 'Active _ record'
  3. Require 'yaml'
  4. Dbconfig = YAML: load (File. open ('database. yml '))
  5. ActiveRecord: Base. establish_connection (dbconfig)
require 'rubygems'require 'active_record'require 'yaml'dbconfig = YAML::load(File.open('database.yml'))ActiveRecord::Base.establish_connection(dbconfig)

Try it

 

Ruby code
  1. Require 'rubygems'
  2. Require 'Active _ record'
  3. Require 'yaml'
  4. Dbconfig = YAML: load (File. open ('database. yml '))
  5. ActiveRecord: Base. establish_connection (dbconfig)
  6. Class User <ActiveRecord: Base
  7. Set_table_name "my_user"
  8. End
  9. Puts User. count
require 'rubygems'require 'active_record'require 'yaml'dbconfig = YAML::load(File.open('database.yml'))ActiveRecord::Base.establish_connection(dbconfig)class User < ActiveRecord::Baseset_table_name "my_user"endputs User.count  

 

If you want to add logs to activerecord, you can do this.

 

Ruby code
  1. Require 'rubygems'
  2. Require 'Active _ record'
  3. Require 'yaml'
  4. Require 'logger'
  5. Dbconfig = YAML: load (File. open ('database. yml '))
  6. ActiveRecord: Base. establish_connection (dbconfig)
  7. ActiveRecord: Base. logger = Logger. new (File. open ('database. log', 'A '))
  8. Class User <ActiveRecord: Base
  9. Set_table_name "my_user"
  10. End
  11. Puts User. count
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::Baseset_table_name "my_user"endputs User.count  

 

In this way, a database. log file is generated.

 

OK. Let's get there today.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.