我有一个 Post 类,包含 TextPost、ImagePost 和 LinkPost 子类(使用 STI)。这些 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 时无法(简单而简洁地)调用父类?