# Rails 2.1.0 - find with :include and missing JOIN in SQL query

**URL:** <https://www.ruby-forum.com/t/rails-2-1-0-find-with-include-and-missing-join-in-sql-query/140401>\
**Category:** Rails\
**Created:** [June 13, 2008, 4:26pm UTC](https://www.ruby-forum.com/t/rails-2-1-0-find-with-include-and-missing-join-in-sql-query/140401 "2008-06-13T16:26:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [June 13, 2008, 4:26pm UTC](https://www.ruby-forum.com/t/rails-2-1-0-find-with-include-and-missing-join-in-sql-query/140401/1 "2008-06-13T16:26:07Z")

</div>

Hi,

We’re trying to rewrite veeeery old Rails app that we successfully  
managed to move to 1.2.6 and now we’re trying with 2.1.0.

One of the problems is caused by :include in AR.find. Rails 1.2.6  
generates totally different SQL query than  
2.1.0. Here’s Rails find query and generated SQL queries:

Foo.find(:all, :include =\> :ticket, :conditions =\> (‘project\_id =\>  
1’))

Rails 1.2.6  
SELECT foos.`id` AS t0\_r0 … FROM foos LEFT OUTER JOIN tickets ON  
tickets.id = foos.ticket\_id WHERE (project\_id = 1) # works fine

Rails 2.1.0  
SELECT \* FROM `foos` WHERE (project\_id = 1) # JOIN is missing and  
Rails complains about missing project\_id column in foos table

Foos table doesn’t have project\_id column, but tickets table does. Foo  
belongs\_to :ticket and Ticket belongs\_to :project.

Any idea how to fix it?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [June 13, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/rails-2-1-0-find-with-include-and-missing-join-in-sql-query/140401/2 "2008-06-13T16:46:14Z")

</div>

On 13 Jun 2008, at 15:25, szimek wrote:

> Foo.find(:all, :include =\> :ticket, :conditions =\> ('project\_id =\>  
> Foos table doesn’t have project\_id column, but tickets table does. Foo  
> belongs\_to :ticket and Ticket belongs\_to :project.
> 
> Any idea how to fix it?

You need to disambiguate your conditions (ie tickets.project\_id = 1)

Fred

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [June 14, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/rails-2-1-0-find-with-include-and-missing-join-in-sql-query/140401/3 "2008-06-14T14:16:03Z")

</div>

On Jun 13, 4:45 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > We’re trying to rewrite veeeery old Rails app that we successfully  
> > SELECT foos.`id` AS t0\_r0 … FROM foos LEFT OUTER JOIN tickets ON
> 
> You need to disambiguate your conditions (ie tickets.project\_id = 1)
> 
> Fred

Thanks! Works perfectly.
