我在Rails中设置了STI(名称已被更改以保护无辜者)
class Mercedes < Car
class BMW < Car
现在在某个阶段我需要从代码中删除对Mercedes
的引用,但是出于历史原因希望保留数据。
当我尝试从cars
表中加载所有记录时,出现此错误:
ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed
to locate the subclass: 'Mercedes'. 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 Car.inheritance_column to use another column for that information.
有没有办法忽略cars
为type
的{{1}}表中的记录?
答案 0 :(得分:3)
您可以在default_scope { where("cars.type != 'Mercedes'") }
课程中添加Car
。
但是:我不建议将这些旧数据留在生产数据库中,如果它会导致您构建这样的解决方法。如果需要保留它,进行存档,然后将其从数据库中删除,或者重新构建数据,以便它可以与您需要进行的任何代码更改一起使用。