ActiveRecord
So, I’ve started playing with Rails, a Ruby MVC web framework. So far, I’ve just barely dipped my toes into it, but I’m really impressed with what I’ve seen so far.
My first stop was ActiveRecord, the database layer for Rails. It’s clearly young, but it’s almost a perfect match for Ruby’s dynamic nature. It uses reflection and database metadata to build table classes on the fly. Here’s a brief example:
#!/usr/bin/ruby
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "localhost",
:username => "user",
:password => "password",
:database => "asterisk")
class Cdr < ActiveRecord::Base
def self.table_name() "cdr"; end
end
Cdr.find_all().each do |cdr|
puts "#{cdr.uniqueid}: #{cdr.channel} -> #{cdr.dstchannel}"
end
This dinky block of code is enough to connect to my Asterisk database and extract call details from the cdr
table. I didn’t need to tell ActiveRecord anything about the structure of the table; it does the right thing on its own.
It’s actually quite a bit more capable then this example shows; it understands relationships and keys, and it has a full set of searching and updating methods. I suspect that I’ll be getting a lot of use out of it.