Read more

STI and LoadError (Expected script/../config/../app/models/sub_model.rb to define SubModel) Exception

Andreas Robecke
August 16, 2012Software engineer

I was using single table inheritance lately and experienced the problem that a parent class did not know about its subclasses in development mode. The inheritance structure was like follows:

class Event < ActiveRecord::Base
end

class GlobalEvent < Event
end

class NewRegistrationEvent < GlobalEvent
end

class ConsultingRequestEvent < GlobalEvent
end
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

When I tried to load all global events in a controller, all I recieved by GlobalEvent.all was an empty set even though there were some new registration events and consulting requests there. To make your parent class aware of its subclasses, you can force your parent class to load the subclass simply by calling require_dependency 'subclass.rb' in your parent class. Now calling GlobalEvent.all will return all events as expected.

Also check out the link below. That guy experienced the same problem within a controller:
when single table inheritance attacks Show archive.org snapshot

Posted by Andreas Robecke to makandra dev (2012-08-16 18:02)