Wednesday, November 14, 2007

ActiveRecord Through ODBC Without Rails

  1. Install Ruby
  2. Install active-record (plus any adapters... such as ODBC) on command line

    gem install active_record --include-dependencies
    gem install odbc-rails --include-dependencies

  3. Write some Ruby code...

The simplest case looks something like this:


# require AR
require 'rubygems' # require gems first since AR is installed via gems
require 'active_record'

# connect to the database (sqlite in this case)
ActiveRecord::Base.establish_connection({
:adapter => odbc,
:dsn => your_pre-configured_odbc_datasource_name,
:username => user_name,
:password => secred_password,
:trace => true,
:convert_numeric_literals => true})

# define your models - this one looks for a table called 'tasks'
class Task < ActiveRecord::Base
end


Then you're good to go! Go ahead and use all that Active Record stuff from a local script...

See here for futher info on using ODBC with rails:

The rails wiki has more on active record outside rails.

No comments: