class Company < ActiveRecord::Base
has_many :company_groups
has_many :groups, :through => :company_groups
end
class CompanyGroup < ActiveRecord::Base
belongs_to :company
belongs_to :group
end
class Group < ActiveRecord::Base
belongs_to :group_type
has_many :company_groups
has_many :companies, :through => :company_groups
end
class GroupType < ActiveRecord::Base
has_many :groups
end
So a company is part of a group and there can be many types of groups.
Is it possible to get a list of distinct group types from the company?
i.e. company.group_types would return an array of the group types that
the particular company is in.
Let me know if you wish me to explain it a bit better
In case anyone has the same problem I did it by using finder_sql:
class Company < ActiveRecord::Base
has_many :company_groups
has_many :groups, :through => :company_groups
has_many :group_types, :finder_sql => ‘SELECT DISTINCT gt.* FROM
group_types gt INNER JOIN groups g ON gt.id = g.group_type_id INNER JOIN
company_groups gc ON gc.group_id = g.id WHERE gc.company_id = #{id}’
end
I’m sure there must be a cleaner way of doing it though…