Rails:无效的单表继承类型错误 [英] Rails: Invalid single-table inheritance type error
问题描述
因此,我正在使用现有数据库迁移此 php 站点,我无法将其更改为 Rails.有一个表:Quotes
,其中有一列名为 type
.每当我尝试创建此模型并设置类型时,它都会告诉我以下错误:
So, I am working on migrating this php site with an existing database which I cannot change over to Rails. There is a table: Quotes
with a column named type
. Whenever I try and create a model of this and set the type, it tells me the following error:
ActiveRecord::SubclassNotFound(单表继承类型无效:HOME 不是 Quotes 的子类)
我不明白为什么它认为它是继承的,因为它不应该继承.我的创建方法如下所示:
I don't understand why it thinks its inheriting because it's not supposed to. My create method looks like this:
quote = Quotes.create(
agent_id: agent.id,
client_id: client.id,
type: 'HOME',
status: 0,
date_created: DateTime.now
)
如果我注释掉类型,一切正常.但是使用类型它会出错.
If I comment out the type, everything works fine. But with the Type it errors.
推荐答案
我通过将模型继承列设置为 nil 解决了这个问题.Active Record Models 可以通过属性 :type
从表继承,将inheritance_column 设置为 nil 会删除该属性,允许您拥有一个名为 type
I resolved this by setting the models inheritance_column to nil. Active Record Models can inherit from a table through the attribute :type
, setting the inheritance_column to nil removes that attribute allowing you to have a database column named type
class Quote < ActiveRecord::Base
self.inheritance_column = nil
end
这篇关于Rails:无效的单表继承类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!