← Back to Upcase

Selecting from a join with same class

(Austin Wang) #1

I’m trying to get a count of how many subcontacts every contact has.

Class Contacts has_many :subcontacts, class_name:
“Contact”,foreign_key: “supercontact_id” belongs_to :supercontact,

And here’s the activerecord part i have so far that’s roughly what i’m trying to do.

Contact.joins{subcontacts.outer}.select(subcontacts.count as subcontact_count)

I think the problem is that the joins portion is looking for a association name and the select part is looking for a table name. The trouble is that the table name is the same table… What’s the best way to do this?