2012-04-28 11 views
2

私は、TextPost、ImagePost、およびLinkPostサブクラス(STIを使用)を持つPostクラスを持っています。これらのPost型は、Post.type(STI規則に従って)の文字列として指定されています。単一のテーブル継承を使用する場合、(サブクラスではなく)親クラスにアクセスできますか?

TextPost.allImagePost.allLinkPost.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を使用するときに親クラスを呼び出すこと(単に&を簡潔にすること)はできませんか?

答えて

1

"text"と等しいタイプの列をデータベースに持つように見えます。 RailsはSTIにtextクラスにしようとしています。タイプ欄にTextPostがあり、textではありません。

+0

よろしくお願いします。 STIでの最初の試みからレコードが残っていた(Post.typeのクラスを 'TextPost'ではなく 'text'として誤ってラベル付けした)。 ありがとうございます。 – jasonmklug

関連する問題