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:projectlogdbdevelopment.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!

If you don't know what %{color:red}Rails% ...", published: true,
 published_at: "2008-07-20 03:52:00", created_at: "2008-07-16 22:56:33",
 updated_at: "2008-07-17 16:21:48">]

At the beginning of the script, ActiveRecord is loaded:

require 'rubygems'
require 'active_record'

The first line loads RubyGems which is a requirement given that you installed ActiveRecord through the gem command. This line is not strictly necessary if you set the RUBYOPT environment variable to -rubygems.

The second line loads ActiveRecord. Notice that you use 'active_record' despite the gem being activerecord, because the file that's being required happens to be called active_record.rb.

Subsequently, you establish a connection to the database:

ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
                                        :database => "blog/db/development.sqlite3",
                                        :timeout => 5000)

If you are working in "standalone" mode, not within a Rails application, you don't have a configdatabase.yml file to rely on; hence, you need to explicitly call the establish_connection class method and pass a hash argument to it, containing the information for the connection. establish_connection is a method that ends up invoking a connection method in the adapter, which in turn invokes a driver method that actually establishes the connection.

Normally, in a Web application, Rails does this for you by invoking that method and passing a hash that's derived from configdatabase.yml. In fact, if you evaluate ActiveRecord::Base.configurations from the Rails console, you'll obtain something along the lines of the following (reformatted for clarity):

{"development" => {"adapter" => "sqlite3","pool" => 5,
                   "timeout" => 5000, "database" => "db/development.sqlite3"},
 "production" =>  {"adapter" => "sqlite3", "pool" => 5,
                   "timeout" => 5000, "database" => "db/production.sqlite3"},
 "test" =>        {"adapter" => "sqlite3", "pool" => 5,
                   "timeout" => 5000, "database" => "db/test.sqlite3"}
}

Rails 2.3 will have a reconnect option for MySQL. By default its value will be false. When set to true, it will attempt to reconnect if the connection is lost.

That's a hash of a hash, so when you're in the (default) development mode, the hash ActiveRecord::Base.configurations['development'] is used as an argument for the establish_connection method.

Within an initializer you can connect to a specific mode by simply referring to it through its symbol. For example, ActiveRecord::Base.establish_connection :my_mode.

Finally, the script defines an Article model, which maps to the articles table, and you immediately put it to good use, by obtaining a list of all the records through the find method:

class Article < ActiveRecord::Base
end

p Article.find(:all)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.107.193