Getting started with Ruby on Rails on Mac OS X

Hivelogic has an excellent writeup to get you all setup and riding the Rails.

A Do-It-Yourself Guide to Installing Ruby, Rails, and FastCGI

Have fun!

How to use Ruby on Rails Active Record from cron

I’m building a Rails web application and am having a great time.

I came across the need to run some Ruby scripts from the command line. While I could have used Ruby/mysql to perform the tasks, I really wanted Active Record access (for obvious reasons).

Here is how you do it:


require 'rubygems'
require_gem 'activerecord'
require '/path/to/your/app/models/category'
require '/path/to/your/app/models/item'

puts 'AR test begin.'
#Connect to the database
ActiveRecord::Base.establish_connection(
    :adapter  => "mysql",
    :host     => "localhost",
    :username => "youruser",
    :password => "yourpwd",
    :database => "yourdb"
)

#turn on AR logging
ActiveRecord::Base.logger = Logger.new('ar_log.txt')

puts 'Finding all categories...'
p Category.find_all
puts 'AR test done.'

The results of a test run:

AR test begin.
Finding all categories...
[#“00000000000000″, “href”=>”", “title”=>”Roman Lit”,
“updated_on”=>”20050404103425″, “type”=>”Category”, “author”=>”",
“lock_version”=>”0″, “id”=>”1″>]
AR test done.

The Active Record log file:

# Logfile created on Mon Apr 04 11:21:03 PDT 2005 by logger.rb/1.5.2.4
  Category Columns (0.000509)   SHOW FIELDS FROM items
  Category Load (0.068370)   SELECT * FROM items WHERE (type = 'Category' )

That’s all there is to it!