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

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?

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

On Jun 13, 4:45 pm, Frederick C. [email protected]
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.