Suppose I have a hierarchy of models using single table inheritance in which the base class inherits from an abstract model:
- BaseModel < ActiveRecord::Base (BaseModel is abstract)
- Person < BaseModel
- Employee < Person
- Contractor < Person
Because of this bug, the following code doesn't work as it should. (Assume the people table has already been populated with Employees and Contractors.)
$ ./script console
Loading development environment.
>> Person.find_first
=> nil
Curiously, Person "learns" about its subclasses as they're used (note that find_first is arbitrary here; any find method elicits the same behavior):
>> Employee.find_first
=> #First employee
>> Person.find_all
=> #All the employees...
>> Contractor.find_first
=> #First contractor
>> Person.find_all
=> #Now, all the employees AND all the contractors
I tracked all of this down to Base#descends_from_active_record?. When building queries, type conditions are only skipped when the superclass is Base; the superclass being an abstract model should, however, also be sufficient to prevent the conditions from being added.
I didn't write any fresh unit tests, but the attached patch seems to address the problem without causing any new ones.