【Rails】「type」というRailsで使ってはいけないカラムがDBにあります。このカラムのデータを利用することはないのですが別の理由で削除したりrenameしたりできません。

下記エラーを飛ばしてtype以外のデータを利用する方法を教えてください。

具体的には下記エラーを解消したいです。
uby-1.9.2-p290 :102 > Book.find(1)
Book Load (0.8ms) SELECT `book`.* FROM `book` WHERE `book`.`id` = 1 LIMIT 1
ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'personal'. 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 Book.inheritance_column to use another column for that information.

回答の条件
  • 1人5回まで
  • 登録:
  • 終了:2012/01/06 09:51:26
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

ベストアンサー

id:techmedia-think No.1

回答回数46ベストアンサー獲得回数13

ポイント310pt

findする際に別名を指定する方法があります。
http://www.ginriki.net/wd/2008/10/02/43/

id:pandeiro_jp

ruby-1.9.2-p290 :111 > Book.find(1, :select => 'id, name, type as etype')
とかでいけました。
ありがとうございます!
Please rename this column if とかでググっても出て来ませんでした。
助かりました!

2012/01/06 09:48:54

その他の回答2件)

id:techmedia-think No.1

回答回数46ベストアンサー獲得回数13ここでベストアンサー

ポイント310pt

findする際に別名を指定する方法があります。
http://www.ginriki.net/wd/2008/10/02/43/

id:pandeiro_jp

ruby-1.9.2-p290 :111 > Book.find(1, :select => 'id, name, type as etype')
とかでいけました。
ありがとうございます!
Please rename this column if とかでググっても出て来ませんでした。
助かりました!

2012/01/06 09:48:54
id:nuna No.2

回答回数32ベストアンサー獲得回数11

ポイント166pt

エラーメッセージに書かれている通りです。
>Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Book.inheritance_column to use another column for that information.

class Book < ActiveRecord::Base
inheritance_column :_type
end

などとすれば STI で使われるカラム名を変更できるので、エラーを回避できます。

id:pandeiro_jp

下記エラーが出ちゃいました・・・

ArgumentError: wrong number of arguments (1 for 0)
from /Users/hogehoge/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:634:in `inheritance_column'
from /Users/hogehoge/fugafuga/app/models/book.rb:4:in `<class:Book>'

2012/01/06 09:51:39
id:taknt No.3

回答回数13539ベストアンサー獲得回数1198

ポイント24pt

http://d.hatena.ne.jp/takuya_1st/20110719/1311061354

http://d.hatena.ne.jp/suer/20090626/1246012135

カラム名に使ってはいけない予約語がありますので
テーブル設計をするときに それを使わないようにするのが
一般的でしょう。

これができないと なると 別名ですが・・・。

id:pandeiro_jp

ありがとうございます。
もちろん最終的にはそうなる予定です!

2012/01/06 09:56:34

コメントはまだありません

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません