LoginSignup
8
6

More than 5 years have passed since last update.

Railsで既存DBのModelを生成する

Posted at

普通にmodelを生成して扱おうと思っていたら
rails mysql2 error table doesn't exist
が出てしまった。解決に手間取ったのでメモ

self.table_nameで解決できる。
まずは、migrationを作成したくないので--migration=falseオプションをつけてモデルを生成する。

$ bundle exec rails g model user --migration=false

そして、モデルクラスに既存のテーブル名を指定する。

class User < ActiveRecord::Base
    self.table_name = 'user'
end
8
6
1

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
8
6