0

I have a STI class hierarchy like so:

Producer, Partner, Freelancer < Statusowner < Contact

When I call e.g. Partner.all I see rails producing this:

SELECT "contacts".* FROM "contacts" WHERE "contacts"."type" IN ('Partner', 'Producer', 'Partner', 'Freelancer') ORDER BY contacts.name

You see that it first includes Partner, and then all Subclasses of Statusowner, including Partner again. This happens similarly with all subclasses.

I have set self.descentants in Statusowner, so everything gets loaded early in the development environment:

class Statusowner < User
  def self.descendants
    [Producer, Sales, Partner, Freelancer]
  end
end

Any idea what I am doing wrong?

1 Answers1

0

probably you simply should not redefine descendants method. maybe if you just require the files after defining the class would be enough.

if you prefer a kind of a hack to autoload maybe you could change your code to:

class Statusowner < User
  MY_DESCENDANTS = [Producer, Sales, Partner, Freelancer]
end

I believe this could make you achieve the same result but this way you don't override the original method. Oh, by the way, the subclasses are inheriting the overroden method, so all these subclasses have the method descendants returning [Producer, Sales, Partner, Freelancer].

formigarafa
  • 377
  • 1
  • 11