LoginSignup
2
2

More than 5 years have passed since last update.

Railsを使わずにActiveRecordを使う方法

Posted at

Railsを使わずにActiveRecordを直接呼び出せると便利です。
このような場合、
1. active_recordをrequireする
2. ActiveRecord::Base.establish_connectionでDBに接続する。
3. ActiveRecord::Base.connection.create_tableなどでテーブル操作を行う。
4. ActiveRecord::Baseを派生させてモデルを作成
5. あとは普通にモデルを操作できます(create/all/findなど)

具体例は以下のとおりです。(paiza.IOを使っているので、実際に変えながら試してみれます。)

https://paiza.io/projects/DgzLdPa3DtwEFyVl9VUFKg
スクリーンショット 2014-11-21 午後6.52.54.png

2
2
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
2