非WEB程序中施用Active Record

非WEB程序中使用Active Record

一、新建一个Ruby文件

 

require "rubygems" 
require "active_record" 

ActiveRecord::Base.establish_connection(:adapter => "oracle", :database => "test_development", :username => "root", :password => "root", :host => "localhost")  
   
class Entry < ActiveRecord::Base  
    
end 

entry = Entry.find(1)  
puts entry.title # Good Life with happy heart

 

二、Rails项目根目录中新建一个Ruby文件

 

require "../config/environment.rb" 

entry = Entry.find(1)  
puts entry.title # Good Life with happy heart