Find_by_contents fails with :include option

find_by_contents will fail if you specify :include in the find_options
parameter. The problem is on line 313:

313: conditions = [ “id in (?)”, id_array ]
314: # combine our conditions with those given by user, if any
315: if find_options[:conditions]
316: cust_opts = find_options[:conditions].dup
317: conditions.first << " and " << cust_opts.shift
318: conditions.concat(cust_opts)
319: end
320: result = self.find(:all,
321: find_options.merge(:conditions => conditions))

Since the id field will be present in two separate tables, it will be
ambiguous and throw an exception, e.g.


Question Load Including Associations (0.000000) Mysql::Error: Column
‘id’ in where clause is ambiguous: SELECT foos.id AS t0_r0, …
bars.id AS t1_r0, … FROM foos LEFT OUTER JOIN bars ON bars.foo_id =
foos.id WHERE (id in (1))

REBUILD YOUR INDEX! One of the id’s didn’t have an associated record: 1

Error should probably log a better message too…

Quick way to fix it:

313: conditions = [ “#{table_name}.#{connection.quote_column_name(‘id’)}
in (?)”, id_array ]

I don’t know if it’s the perfect fix, but it works for me.

Cheers,
Aaron

On Thu, Aug 17, 2006 at 10:07:29PM +0200, Aaron P. wrote:

find_by_contents will fail if you specify :include in the find_options
parameter. The problem is on line 313:

this problem has already been fixed in trunk.

Jens


webit! Gesellschaft für neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer [email protected]
Schnorrstraße 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66