6
5

More than 5 years have passed since last update.

RubyからActiveRecord経由でRedshiftを使う

Last updated at Posted at 2014-09-11

前提条件

ActiveRecordのVersion4以上

Gemfileの編集

$ vi Gemfile
gem 'pg_typecast', '~> 0.1.3'
gem 'activerecord4-redshift-adapter', github: 'aamine/activerecord4-redshift-adapter'

redshift-adapterでActiveRecordをRedshift対応にする
pg_typecastを使わないと、戻り値の数値が文字列になってしまうため追加する

データベースの設定

config.yml
  database:
    adapter: redshift
    encoding: utf8
    host: <Endpoint>
    database: <Database Name>
    username: <your username>
    password: <your password>
    port: 5439

デモ

$ vi ./demo.rb
demo.rb
#!/usr/bin/env ruby
Bundler.require
sql = "SELECT NOW()"
puts ActiveRecord::Base.connection.execute(sql).first
$ bundle exec ruby ./demo.rb

出力結果

D, [2014-09-11T14:27:25.801032 #17829] DEBUG -- :    (16.5ms)  SELECT NOW()
{:now=>2014-09-11 14:27:25 +0900}

参考

pg_typecast
activerecord-redshift-adapter

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5