Drink or code ... Why should you choose ?
Published at Sun, 08 Mar 2009 22:00:00 +0000

Ruby : use Active Record without Rails

If you've already been using Active Record. And as you're reading this blog and this article, I suppose you have, even though you may not know you have, you know how it is powerful. In case your memory fails you, active record is the list of methods used to access databases in Ruby on Rails. It is compatible with MySQL, SQLite and PostgreSQL (and if you wish to add your own adaptor, you can quite easily). But Rails is a framework for web applications. And God knows the web isn't the only face of computer programming. So we'll see how easy it is to use Active Record without Rails (or without all rails as activerecord is a part of it). First of all, you need to install the library. If you already Rails installed on your server, you're ready to go. Otherwise, do the following :

It'll get the latest stable version of the library and install it on your computer.

Now, let's start having fun with some code lines :)

Create a new document named (for example) main.rb.

First, you need to call the rubygems and activerecord libraries.

require 'rubygems'
require 'active_record'

With those lines at the top of your document, you now have access to all the methods provided by Active Record.

So let's use them.

ActiveRecord::Base.establish_connection(
    :adapter => 'mysql',
    :host => 'localhost',
    :user => 'root',
    :password => 'root',
    :database => 'test'
)

This will instantiate the library. <em>However, it'll not connect you to the database. You're getting connected only when you do the first query</em>

We now need to create a model. I invite you not to create it in the same document. So create a document named "user.rb" in the same directory as your main.rb. Put in that new document :

class User > ActiveRecord::Base
end

And in your main.rb, call it.

require 'user'

You now have access, in your main.rb, to the object User, which is a child of ActiveRecord::Base and grants it all the usual methods available to Ruby on Rails models. However, you're not in a rails application. So let's get all our users.

p User.find(:all)

Based on the fact that the table "users" exists in your database "test", this will print in your console the hash of all your users.

You can now do anything you want using ActiveRecord in your Ruby console or offline user interfaces applications.

But wait it's not over. Until now, we've defined all our configuration datas directly in the program. And that's very bad. So we need to add an external database.yml document.

adapter: mysql
host: localhost
username: root
password: root
database: refcrawler_dev

And to load that external document in ours to get it's values.

require 'yaml'
dbconfig = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)

Comments

Rob
Rob says: Mon, 25 Jul 2011 04:28:08 +0000

Typos hurt everyone

class User > ActiveRecord::Base end

Should be

class User < ActiveRecord::Base end

Those evil greater and less than sign....

Post a New Comment

Markdown enabled