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.