Incorporating Rails Components | 285
ETL operations
ActiveRecord can be a useful tool to load data into and extract data from databases.
It can be used for anything from one-off migration scripts to hourly data transforma-
tion jobs. The following is a representative example, using James Edward Gray II’s
FasterCSV library:
require 'rubygems'
require 'fastercsv' # gem install fastercsv
require 'active_record'
# Set up AR connection and define User class
ActiveRecord::Base.establish_connection(
# (connection spec)...
)
class User
# The table we're importing into doesn't use Rails conventions,
# so we'll override some defaults.
set_table_name 'user'
set_primary_key 'userid'
end
FasterCSV.foreach('users.csv', :headers => true) do |row|
# The CSV header fields correspond to the database column names,
# so we can do this directly, with no mapping.
User.create! row.to_hash
end
Schema operations
ActiveRecord’s migration methods can be used as a portable abstraction for SQL
data definition language (DDL). TheActiveRecord::Schema.definefunction allows
you to use the ActiveRecord schema definition statements within a block to perform
operations on a database. The full set of DDL operations is documented in
ActiveRecord::ConnectionAdapters::SchemaStatements.
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
# (connection spec)...
)
ActiveRecord::Schema.define do
create_table :sites do |t|
t.string :name, :null => false
t.string :city
t.string :state
end
add_column :users, :site_id, :integer
add_index :users, :site_id
end