Some of you must be wondering, I must be insane to use ActiveRecord outside of Rails. But there are some very compelling reasons.
- ActiveRecord is an awesome library for Object Relational Mapping (ORM).
- Other projects might not involve the use of Rails.
- You want to experiment with ActiveRecord prior to creating a plugin for your Rails project.
- A library for ORM is already written (why reinvent the wheel?)
- Your just very bored.
Whatever the case may be, it’s best to be prepared.
If you already have Rails installed via RubyGems, then your already set to go. Else install it in the terminal via:
sudo gem install activerecord
Playing with ActiveRecord
Open up a file, and insert the following:
require 'rubygems' require 'active_record' ActiveRecord::Base.establish_connection( :adapter => 'mysql', :database => 'database', :username => 'user', :password => 'password', :host => 'localhost')
Now if you are familiar with Rails, this is the same configuration as seen in your
database.yml file (
RAILS_ROOT/config/database.yml ). There are more options for how to connect to a database, just check the
Moving Database Configuration to Another File
Note: It’s not safe to save your database configuration in the same file as you code. In case you distribute your code, and forget to remove your password. That wouldn’t be a cool scenario. (Well it would also be pointless if you sent your database configuration file as well).
So lets make a
database.yml and dump the information in there.
adapter: mysql database: database username: user password: password host: localhost
Now our previous file that utilizes ActiveRecord will look like such:
require 'rubygems' require 'active_record' require 'yaml' dbconfig = YAML::load(File.open('database.yml')) ActiveRecord::Base.establish_connection(dbconfig)
Playing with ActiveRecord Safely!
And now your set to playing around with ActiveRecord without Rails!
require 'rubygems' require 'active_record' require 'yaml' dbconfig = YAML::load(File.open('database.yml')) ActiveRecord::Base.establish_connection(dbconfig) class User < ActiveRecord::Base end puts User.count # 6
Debugging ActiveRecord SQL Queries
Sometimes you run into problems, and suspect your SQL isn’t generated as it’s supposed to be. But how do we debug this?
require 'rubygems' require 'active_record' require 'yaml' require 'logger' dbconfig = YAML::load(File.open('database.yml')) ActiveRecord::Base.establish_connection(dbconfig) ActiveRecord::Base.logger = Logger.new(STDERR) class User < ActiveRecord::Base end puts User.count # SQL (0.000277) SELECT count(*) AS count_all FROM users # 6
This will output it to standard error (in which most cases its) your screen. Sadly this can also clutter your screen.
Debugging ActiveRecord SQL Queries to a File
Now if you know your shell well, you can pipe it to a file via:
ruby connect.rb 2>> database.log
But you may forget to pipe it, or what not.
So replace the line containing
ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'a'))
Warning: I chose to append (‘a’) the output to the file. This may result in very large files. You can chose to use overwrite (‘w’) instead.
From here on you can watch the file with:
tail -f database.log [image16x16:terminal].
Note: If you view the
database.log file, you’ll notice the following. This is because ActiveRecord colorizes output for viewing in a terminal. But when viewed as a normal file, you get garbage.
^[[4;36;1mSQL (0.000273)^[[0m ^[[0;1mSELECT count(*) AS count_all FROM users ^[[0m
To disable colorize logging, enter:
ActiveRecord.colorize_logging = false