April 2009
Intermediate to advanced
479 pages
12h 55m
English
Much like ADO.NET, ActiveRecord doesn't have to be used only in Web applications. You can load it in any program or script you are writing. Listing 7-1 shows an example of a small script that employs ActiveRecord.
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
:database => "blog/db/development.sqlite3",
:timeout => 5000)
class Article < ActiveRecord::Base
end
p Article.find(:all)
|
When you save this file as listing0701.rb in C:\projects and run it, the array of existing articles in the articles table within the C:\project\blog\db\development.sqlite3 database is printed in the output as follows:
[#<Article id: 1, title: "Hello, Rails!", body: "Hi from the body of an article. :)", published: false, published_at: "2008-07-11 09:24:00", created_at: "2008-07-11 09:32:41", updated_at: "2008-07-17 03:18:28">, #<Article id: 2, title: "Lorem Ipsum", body: "Lorem ipsum dolor sit amet, consectetuer adipiscing...", published: true, published_at: "2008-07-17 06:36:00", created_at: "2008-07-16 14:31:33", updated_at: "2008-07-20 20:20:30">, #<Article id: 3, title: "More Lorem Ipsum", body: "Etiam justo justo, ultricies sed, semper ac, hendre...", published: true, published_at: "2008-07-17 01:50:00", created_at: "2008-07-16 14:35:18", updated_at: "2008-07-17 01:53:05">, #<Article id: 4, title: "Oh hi!", body: "Hi there!\r\n\r\nIf you don't know what ...