Вопрос

I have a STI setup in Rails (names have been changed to protect the innocent)

class Mercedes < Car

class BMW < Car

Now at some stage I need to remove references to Mercedes from the code, but want to leave the data in place for historical reasons.

When I try to load all records from the cars table, I get this error:

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.

Is there a way to ignore records in the cars table whose type is Mercedes?

Это было полезно?

Решение

You could add a default_scope { where("cars.type != 'Mercedes'") } to your Car class.

HOWEVER: I would not advise leaving this old data in your production database if it's going to cause to you build in workarounds like this. If you need to keep it, make an archive, and then remove it from the db, or restructure the data so it works with whatever code changes you need to make.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top