# ActiveRecord inner joins

**URL:** <https://www.ruby-forum.com/t/activerecord-inner-joins/132021>\
**Category:** Rails\
**Created:** [March 15, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/activerecord-inner-joins/132021 "2008-03-15T13:58:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 15, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/activerecord-inner-joins/132021/1 "2008-03-15T13:58:20Z")

</div>

I have these models:

User -\> Order -\> Registration \<- Camp

So a user has many orders, orders have many registrations and camps have  
many registrations.

I have all the associations written up correctly, but I want to know how  
to get all the users for a particular camp.

i.e.

@camp = Camp.find\_by\_name(“band camp”)  
mycampers = @camp.orders.registrations.users

naturally, this doesn’t work – do i have to use sql here? the :include  
option to find looks good, but I can’t get it to work.

best,

tim

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 15, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/activerecord-inner-joins/132021/2 "2008-03-15T14:44:23Z")

</div>

Tim B. wrote:

> i.e.
> 
> @camp = Camp.find\_by\_name(“band camp”)  
> mycampers = @camp.orders.registrations.users
> 
> naturally, this doesn’t work – do i have to use sql here? the :include  
> option to find looks good, but I can’t get it to work.

Something like

User.find :all, :select =\> ‘distinct users.\*’,  
:joins =\> {:orders =\> {:registrations =\> :camp}},  
:conditions =\> [‘camps.name = ?’, camp\_name]

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 15, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/activerecord-inner-joins/132021/3 "2008-03-15T15:06:34Z")

</div>

Tim B. wrote:

> User -\> Order -\> Registration \<- Camp
> 
> So a user has many orders, orders have many registrations and camps have  
> many registrations.
> 
> I have all the associations written up correctly, but I want to know how  
> to get all the users for a particular camp.

class Camp \< ActiveRecord::Base  
has\_many :registrations  
has\_many :orders, :through =\> :registrations, :uniq =\> true, :include  
=\> :user  
:

@camp = Camp.find\_by\_name(“band camp”)  
mycampers = @camp.orders.map {|o| o.user}.uniq

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 15, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/activerecord-inner-joins/132021/4 "2008-03-15T16:27:17Z")

</div>

Mark B. wrote:

> Tim B. wrote:
> 
> > User -\> Order -\> Registration \<- Camp
> > 
> > So a user has many orders, orders have many registrations and camps have  
> > many registrations.
> > 
> > I have all the associations written up correctly, but I want to know how  
> > to get all the users for a particular camp.
> 
> class Camp \< ActiveRecord::Base  
> has\_many :registrations  
> has\_many :orders, :through =\> :registrations, :uniq =\> true, :include  
> =\> :user  
> :
> 
> @camp = Camp.find\_by\_name(“band camp”)  
> mycampers = @camp.orders.map {|o| o.user}.uniq

this is great! thanks for your help. is :uniq a custom method of  
has\_many :through ?

best,

tim

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 15, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/activerecord-inner-joins/132021/5 "2008-03-15T18:12:11Z")

</div>

Tim B. wrote:

> this is great! thanks for your help. is :uniq a custom method of  
> has\_many :through ?

You can use “:uniq =\> true” for any “has\_many” (and HABTM) association,  
however it is most useful for “:through” types since direct associations  
that need to be unique collections can be handled in the model  
validation to prevent duplicates in the first place.
