【发布时间】:2014-10-09 14:36:09
【问题描述】:
我有一个新的 Rails 4 应用,我在 STI 配置中创建了几个模型。
主模型名为Order,它继承自ActiveRecord::Base。看起来是这样的:
class Order < ActiveRecord::Base
TYPES = %w(remote local)
scope :remotes, -> { where(type: 'remote') }
scope :locals, -> { where(type: 'local') }
end
另外两个模型位于models/orders 的一个文件夹中,它们被称为Remote 和Local,它们都继承自Order
订单迁移文件如下所示:
def change
create_table :orders do |t|
t.string :source_url, null: false
t.string :final_url, null: false
t.string :type
t.string :category
t.timestamps
end
end
我还通过以下方式确保将 models/orders 目录包含到 Rails 中:
config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]
现在,当我使用空数据库登录控制台并运行 Order.all 时,一切都很好,我得到一个空关系对象。但是在我创建第一个 Remote 对象并尝试再次运行 Order.all 后,我收到以下错误:
>> Order.all
Order Load (1.0ms) SELECT "orders".* FROM "orders"
ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate
the subclass: 'Remote'. 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
Order.inheritance_column to use another column for that information.
这里发生了什么?我做错了什么?
提前致谢。
【问题讨论】:
-
Remoteclass 你看起来怎么样? -
远程类
-
你为什么要花这么多精力自己操作类型列? Rails 将完全为您处理...
标签: ruby-on-rails ruby activerecord single-table-inheritance