# Find with includes, conditions on included tables, and limit

**URL:** <https://www.ruby-forum.com/t/find-with-includes-conditions-on-included-tables-and-limit/147684>\
**Category:** Rails\
**Created:** [September 4, 2008, 9:25am UTC](https://www.ruby-forum.com/t/find-with-includes-conditions-on-included-tables-and-limit/147684 "2008-09-04T09:25:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Artifact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@Artifact](https://www.ruby-forum.com/u/Artifact)\
**Post date:** [September 4, 2008, 9:25am UTC](https://www.ruby-forum.com/t/find-with-includes-conditions-on-included-tables-and-limit/147684/1 "2008-09-04T09:25:46Z")

</div>

I have my main Night model, which has and belongs to many Users and  
Genres. I’m trying to put a limit on my find statement, but it causes  
SQL problems. Here’s what I’m trying to do:

conditions = [“genres\_nights.genre\_id IN (:genres) AND venue\_id IN  
(:venues) AND nights\_users.user\_id IN (:users) AND date \>= :date”,  
{:genres =\> genres, :venues =\> venues, :users =\> users, :date =\>  
date}]  
find(:all, :include =\> [:venue, :genres, :users, {:flyer  
=\> :thumbnails}], :conditions =\> conditions, :limit =\> 16)

The problem is that the limit option is forcing rails to split the  
query into two, first without the joins, where it finds the IDs of the  
records. It should then do another query to retireve the data on the  
records with those IDs. This is because using limit on a query with  
joins doesn’t work due to the extra rows from the joins.

Anyway, the long and short of it is that the limit means that the  
joins are not included in the initial “Load IDs for Limited Eager  
Loading” query, but that query still references the now non-joined  
tables in its conditions, producing the following error:

Night Load IDs For Limited Eager Loading (0.000000)  
SQLite3::SQLException: no such column: nights\_users.user\_id: SELECT id  
FROM “nights” WHERE (genres\_nights.genre\_id IN (NULL) AND venue\_id IN  
(NULL) AND nights\_users.user\_id IN (NULL) AND date \>= ‘2008-09-04’)  
LIMIT 16

So, what am I to do? Maybe I should use :join to manually specifiy the  
joins instead of using :include, but won’t I lose out on some rails  
magic that way? If that is a good option, any hints on how to do it?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Artifact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@Artifact](https://www.ruby-forum.com/u/Artifact)\
**Post date:** [September 4, 2008, 9:32am UTC](https://www.ruby-forum.com/t/find-with-includes-conditions-on-included-tables-and-limit/147684/2 "2008-09-04T09:32:14Z")

</div>

On 4 Sep 2008, at 05:55, Artifact wrote:

> =\> :thumbnails}], :conditions =\> conditions, :limit =\> 16)  
> tables in its conditions, producing the following error:
> 
> Night Load IDs For Limited Eager Loading (0.000000)  
> SQLite3::SQLException: no such column: nights\_users.user\_id: SELECT id  
> FROM “nights” WHERE (genres\_nights.genre\_id IN (NULL) AND venue\_id IN  
> (NULL) AND nights\_users.user\_id IN (NULL) AND date \>= ‘2008-09-04’)  
> LIMIT 16

That’s a bug in rails - it hasn’t worked out that you were referencing  
the join tables.  
Do you actually need :include? You can use the same syntax  
with :joins, it just won’t try and be clever and uess which tables it  
needs to join.

Fred

---

<div class="post-metadata">

**Author:** ![Artifact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@Artifact](https://www.ruby-forum.com/u/Artifact)\
**Post date:** [September 10, 2008, 10:11am UTC](https://www.ruby-forum.com/t/find-with-includes-conditions-on-included-tables-and-limit/147684/3 "2008-09-10T10:11:49Z")

</div>

Aha, thanks for that, changing :include to :join works fine. I didn’t  
see the line in the docs that tells me i can do that. That’ll teach me  
to read more carefully.

If this is a bug, is there a ticket somewhere for it i could keep an  
eye on?

A

On Sep 4, 8:31 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Artifact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@Artifact](https://www.ruby-forum.com/u/Artifact)\
**Post date:** [September 10, 2008, 10:35am UTC](https://www.ruby-forum.com/t/find-with-includes-conditions-on-included-tables-and-limit/147684/4 "2008-09-10T10:35:52Z")

</div>

On 10 Sep 2008, at 09:11, Artifact wrote:

> Aha, thanks for that, changing :include to :join works fine. I didn’t  
> see the line in the docs that tells me i can do that. That’ll teach me  
> to read more carefully.
> 
> If this is a bug, is there a ticket somewhere for it i could keep an  
> eye on?

Don’t know, then again I didn’t look 🙂

Fred

---

<div class="post-metadata">

**Author:** ![Artifact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@Artifact](https://www.ruby-forum.com/u/Artifact)\
**Post date:** [September 10, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/find-with-includes-conditions-on-included-tables-and-limit/147684/5 "2008-09-10T12:01:57Z")

</div>

I should metion this for the benefit of anyone else with the same  
problem: The problem disappears, ie. :include works, when I use mysql  
rather than sqlite3.

On Sep 10, 9:35 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
