Skip to Content
Ruby on Rails® for Microsoft Developers
book

Ruby on Rails® for Microsoft Developers

by Antonio Cangiano
April 2009
Intermediate 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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

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

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

Michael Hartl
Rails 4 in Action

Rails 4 in Action

Yehuda Katz, Ryan Bigg, Rebecca Skinner, Steve Klabnik
PHP 7: Real World Application Development

PHP 7: Real World Application Development

Doug Bierer, Altaf Hussain, Branko Ajzele

Publisher Resources

ISBN: 9780470374955Purchase book