【Rails3】カラム名に使ってはいけない予約語を調べる

カラム名に「type」を使ったところ、以下のようなエラーが出てしまいました。

ActiveRecord::SubclassNotFound (The single-table inheritance mechanism failed to locate the subclass: 'news'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Site.inheritance_column to use another column for that information.):
app/controllers/sites_controller.rb:5:in `index'

typeが予約語だからだそうです。

予約語って色々あると思いますが、ぱっと思いつくのがメソッド名。
Modelで定義されているメソッドをカラム名に使ってしまうと、
メソッド名にバッティングが起きてしまい、このためエラーとなるようです。

では、Object#methods を使ってそのメソッド名を調べてみましょう。
カラムのないモデルを作成して、以下のコマンドを実行します。

$ rails console
$ a = (モデルクラスの名前).new
$ fo = open('methods.txt', 'w')
$ fo.print(a.methods.join("\n"))
$ fo.close


以上で、モデルクラスで使われているメソッド名がmethods.txtに保存されます。
合計230個ほどあります。('type'も含まれていますね。。´A`#)

他にもうっかり使ってしまいそうな名称として

  • reload
  • send
  • methods
  • from_xml
  • destroy
  • load
  • hash
  • class
  • touch
  • errors
  • changed
  • try
  • save
  • inspect

などがありました。注意しましょう。

他の予約語の調べ方、ご存知ないですか?情報お待ちしてます(^q^)