[RAILS] Why does AR use base_class for polymorphic relationship "type"

I have a couple models that inherit from a base class that then
inherits from AR.

When they are used in a polymorphic relationship the “type” is always
the base class.

Just curious if anyone has a reason why it doesn’t use the actual
class rather then the base_class. I have patched AR to use the actual
class if !base_class.table_exists? but it adds another query each time
(which in turn fails some of the rails tests).

My only guess was for use with STI, but even then I am not sure if
there is a better solution then using the base_class

-Eric

On Apr 5, 5:54am, FuelXC [email protected] wrote:

My only guess was for use with STI, but even then I am not sure if
there is a better solution then using the base_class

By default, Rails stores the class name that corresponds to the
table name that the record is from (to avoid storing duplicate data
for STI subclasses). If you’re actually storing the subclasses in
different tables, you’ll probably want to do ‘self.abstract_class =
true’ in the base class to make this clearer.

–Matt J.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs