I have a Leader that inherits from Person
class Leader < Person
self.table_name = 'person'
end
end
Person has_many :email_addresses
But when I look up
Leader.first.email_addresses.to_sql
I get
SELECT `email_address`.* FROM `email_address` WHERE `email_address`.`entity_id` = 4 AND `email_address`.`entity_type` = 'Leader' ORDER BY `email_address`.`position` ASC
AR is using Leader
as the type, but my all my email_addresses have Person
as the type. Is there a way to define this manually so that the Leader
class's "type" is always the base class?
Addendum:
I have a workable solution as follows, that I'm not in love with, and that is to delegate the assoc to the base class instance:
class Person < ApplicationRecord
# Allows subclasses to access/delegate to the superclass instance
# especially necessary for polymorphic assocs where the classname needs to stay Person
protected
def person_object
Person.find(id)
end
end
class Leader < Person
delegate :email_addresses, to: :person_object
end