加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 单表继承在Rails 4 app中不起作用 – ActiveRe

发布时间:2020-12-17 02:46:55 所属栏目:百科 来源:网络整理
导读:我有一个新的Rails 4应用程序,我在STI配置中创建了一些模型. 主模型称为Order,它继承自ActiveRecord :: Base.这是它的样子: class Order ActiveRecord::Base TYPES = %w(remote local) scope :remotes,- { where(type: 'remote') } scope :locals,- { where
我有一个新的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

其他两个模型位于模型/订单的文件夹中,它们被称为远程和本地,它们都从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

我还确保通过执行以下操作将模型/订单目录包含在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.

这里发生了什么?我做错了什么?

提前致谢.

解决方法

为了调试这个,我只是简单地看看我从两个实验中学到的东西……

首先,即使只是暂时的,也要将子类移出Orders文件夹.可能是您将它们嵌套在模块中,或者轨道期望它们基于名称,因此“类型”列与您认为应该匹配的列不匹配.

第二,我尝试从命令行创建一个子类,持久化它,并查看ActiveRecord在该类型列中放置的内容,看它是否与您期望的相匹配.

我怀疑这与模型下的子文件夹有关,而Rails无法在类型指定的情况下找到类.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读