# Has\_many :through question

**URL:** <https://www.ruby-forum.com/t/has-many-through-question/90075>\
**Category:** Rails\
**Created:** [March 25, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/has-many-through-question/90075 "2007-03-25T20:41:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kule](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecccb3/32.png) [@kule](https://www.ruby-forum.com/u/kule)\
**Post date:** [March 25, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/has-many-through-question/90075/1 "2007-03-25T20:41:36Z")

</div>

I have the following setup:

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

Thanks  
Luke

---

<div class="post-metadata">

**Author:** ![kule](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecccb3/32.png) [@kule](https://www.ruby-forum.com/u/kule)\
**Post date:** [March 27, 2007, 11:10am UTC](https://www.ruby-forum.com/t/has-many-through-question/90075/2 "2007-03-27T11:10:42Z")

</div>

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…

Cheers  
Luke

---

<div class="post-metadata">

**Author:** ![kule](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecccb3/32.png) [@kule](https://www.ruby-forum.com/u/kule)\
**Post date:** [March 28, 2007, 12:55am UTC](https://www.ruby-forum.com/t/has-many-through-question/90075/3 "2007-03-28T00:55:27Z")

</div>

According to my book (agile web development with rails 2nd ed)…

class Company \< ActiveRecord::Base  
has\_many :company\_groups  
has\_many :groups, :through =\> :company\_groups  
has\_many :groups\_types, :through =\> :company\_groups, :unique =\>  
true  
end

or

class Company \< ActiveRecord::Base  
has\_many :company\_groups  
has\_many :groups, :through =\> :company\_groups  
has\_many :groups\_types, :through =\> :company\_groups, :select =\>  
“distinct groups.\*”  
end

Sorry about the timing, this group is too large to read daily.  
On Mar 27, 9:10 pm, Luke P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kule](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecccb3/32.png) [@kule](https://www.ruby-forum.com/u/kule)\
**Post date:** [March 28, 2007, 11:28am UTC](https://www.ruby-forum.com/t/has-many-through-question/90075/4 "2007-03-28T11:28:28Z")

</div>

reed wrote:

> According to my book (agile web development with rails 2nd ed)…
> 
> class Company \< ActiveRecord::Base  
> has\_many :company\_groups  
> has\_many :groups, :through =\> :company\_groups  
> has\_many :groups\_types, :through =\> :company\_groups, :unique =\>  
> true  
> end
> 
> or
> 
> class Company \< ActiveRecord::Base  
> has\_many :company\_groups  
> has\_many :groups, :through =\> :company\_groups  
> has\_many :groups\_types, :through =\> :company\_groups, :select =\>  
> “distinct groups.\*”  
> end
> 
> Sorry about the timing, this group is too large to read daily.  
> On Mar 27, 9:10 pm, Luke P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks, I not sure thats what I was after though.

I’m after a unique set of group types not groups.

e.g.

groups table:

## id group\_type\_id name

1 1 a  
2 1 b  
3 2 x  
4 2 y

A company in groups b & x should return group\_types with ids [1,2] and a  
company in groups a & b should return a group\_type with id [1].

---

<div class="post-metadata">

**Author:** ![kule](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecccb3/32.png) [@kule](https://www.ruby-forum.com/u/kule)\
**Post date:** [March 28, 2007, 2:31am UTC](https://www.ruby-forum.com/t/has-many-through-question/90075/5 "2007-03-28T02:31:38Z")

</div>

Good lord, I think it’s too large/active to +skip+ a day!

RSL
