2

TextPost、ImagePost、およびLinkPostサブクラス(STIを使用)を持つPostクラスがあります。これらのPostタイプは、Post.type(STI規則に従って)の文字列として指定されます。

TextPost.all、、と呼べますが、問題ImagePost.allありませんLinkPost.all

まだ電話できると思っていましPost.allたが、次のエラーが発生します。

ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'text'. 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 Post.inheritance_column to use another column for that information.

参考までに、これが私のschema.rbの関連部分です。

create_table "posts", :force => true do |t|
  t.string   "title"
  t.string   "type"
  t.integer  "author_id"
  t.datetime "publish_datetime"
  ...
end

そして私のサブクラス(それぞれが適切な名前の.rbファイルにあります):

class TextPost < Post
  ...
end

class ImagePost < Post
  ...
end

class LinkPost < Post
  ...
end

私は何か間違ったことをしていますか?または、STIを使用しているときに親クラスを(単純かつ簡潔に)呼び出すことはできませんか?

4

1 に答える 1

1

データベースに「テキスト」と等しいタイプ列の行があるようです。RailsはそれをtextクラスにSTIしようとしています。TextPost必要なものは、ではなくタイプ列にあるように見えますtext

于 2012-04-28T02:45:01.730 に答える