# Joins in Rails

**URL:** <https://www.ruby-forum.com/t/joins-in-rails/89325>\
**Category:** Rails\
**Created:** [March 19, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/joins-in-rails/89325 "2007-03-19T16:48:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [March 19, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/joins-in-rails/89325/1 "2007-03-19T16:48:50Z")

</div>

Hi guys, I have a find statement written like so in my event model:

@events = find(:all, :conditions =\> ["((? BETWEEN start\_on AND end\_on)  
OR (? BETWEEN start\_on AND end\_on)) AND id != ?",  
start\_time,end\_time,event\_id])

but I would like to add to this, to only return the results where the  
event id and (an additional) user id appear in another link-table called  
subscriptions.

My event model has the following defined at the top:

class Event \< ActiveRecord::Base  
has\_many :subscriptions, :dependent =\> :destroy  
has\_many :users, :through =\> :subscriptions

and likewise the subscription model has the following defined at the  
top:

class Subscription \< ActiveRecord::Base  
belongs\_to :events  
belongs\_to :users

I could probably hard code it into the :conditions in the above find  
method call above but this seems wrong - any ideas?

Cheers  
Mick

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [March 19, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/joins-in-rails/89325/2 "2007-03-19T17:44:26Z")

</div>

I may be missing something but have you tried:

Event.find(:all, :conditions =\> […], :include =\> :users)

I am unsure whether this would work with a has\_many :through but it is  
worth a shot. At the very least you should be able to :include =\>  
:subscriptions and collect your users from that.

-Shawn

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [March 19, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/joins-in-rails/89325/3 "2007-03-19T17:55:02Z")

</div>

Shawn Roske wrote:

> I may be missing something but have you tried:
> 
> Event.find(:all, :conditions =\> […], :include =\> :users)
> 
> I am unsure whether this would work with a has\_many :through but it is  
> worth a shot. At the very least you should be able to :include =\>  
> :subscriptions and collect your users from that.
> 
> -Shawn

Hmmm, I tried that and it doesnt work. The functionality I am trying to  
creat is to display a list of a user’s subscriptions and check against  
any other event to see if there are 2 events which clash (time-wise).

But currently it returns all events which clash, regardless of whether  
the user has subscribed (i.e. has a record stored in the subscriptions  
table).

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [March 19, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/joins-in-rails/89325/4 "2007-03-19T19:48:29Z")

</div>

Ok, I see, looking at

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000992)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

:include gives a LEFT OUTER JOIN which you don’t want, you should be  
able to use :join to do this, e.g.:

Event.find(:all, :conditions =\> […], join =\> “INNER JOIN  
subscriptions ON subscriptions.event\_id = id”)

There may be a nicer rails-ish way of doing this, but I am unaware of  
it.

-Shawn
