LoginSignup
2
3

More than 5 years have passed since last update.

active record migrationを他言語のフレームワークに導入する方法

Last updated at Posted at 2015-11-22

Railsで使われているmigration機能は便利なのでほかのフレームワークに使いたい場合は以下の方法でできます
データベースはpostgresを使用

  • gemfileを作成
source 'https://rubygems.org'

ruby '2.2.2'

gem 'pg'
gem "activerecord"
gem 'active_record_migrations'
  • ライブラリをインストールする
$ bundle install

今現在最新(2015年11月時点)rubyのversionが2.2.3だと下記のエラーになります

NoMethodError: private method `load' called for Psych:Module

なので2.2.2を使用

  • ルートパスにRakefileを作成して下記を記入
Rakefile
require 'active_record_migrations'
ActiveRecordMigrations.load_tasks
  • ルートパスにdbフォルダを作成して、dbフォルダの中にmigrateフォルダを作成

dbフォルダにrailsと同じ要領でymlファイルを作成、初期の状態だとconfig.ymlというファイル名で読み込むようになっているのでconfig.ymlを作成

config.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: hoge_development

test:
  <<: *default
  database: hoge_test

production:
  <<: *default
  database: hoge_production
  • 以下のコマンドを打って作成
$ rake db:create

migrationの作成等はRailsと同じなので省略します

  • rakeコマンドを使用した時に下記のエラーがでたらBigDecimalをuninstallする
: warning: already initialized constant BigDecimal::NAN
  • アンインストール
$ gem uninstall bigDecimal
2
3
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
2
3