Skip to Content
Ruby on Rails® for Microsoft Developers
book

Ruby on Rails® for Microsoft Developers

by Antonio Cangiano
April 2009
Intermediate to advanced content levelIntermediate to advanced
479 pages
12h 55m
English
Wrox
Content preview from Ruby on Rails® for Microsoft Developers

7.2. ActiveRecord Outside of Rails

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.

Example 7.1. A Sample Script That Uses 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby on Rails™ 3 Tutorial: Learn Rails™ by Example

Ruby on Rails™ 3 Tutorial: Learn Rails™ by Example

Michael Hartl

Publisher Resources

ISBN: 9780470374955Purchase book