# Random | in join statement with more than one :include

**URL:** <https://www.ruby-forum.com/t/random-in-join-statement-with-more-than-one-include/57338>\
**Category:** Rails\
**Created:** [April 10, 2006, 3:39am UTC](https://www.ruby-forum.com/t/random-in-join-statement-with-more-than-one-include/57338 "2006-04-10T03:39:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [April 10, 2006, 3:39am UTC](https://www.ruby-forum.com/t/random-in-join-statement-with-more-than-one-include/57338/1 "2006-04-10T03:39:20Z")

</div>

I have an ActiveRecord class that has several has\_many,belongs\_to,  
habtm, and the new has\_many =\> :through relationships.

Whenever I try a

find(:all, :conditions =\> “some conds”, :include [:relationship])

it works fine, but if I have

find(:all, :conditions =\> “some conds”, :include [:relationship,  
:other\_relation])

it gets a sql error such as

Mysql::Error: You have an error in your SQL syntax; check the manual  
that corresponds to your MySQL server version for the right syntax to  
use near …

I’ll paste the actual output below, but what I’ve found is that there is  
a stray pipe character ( | ) near the LEFT OUTER JOIN statements. I  
have no idea why its there, but if I remove it and paste the result into  
the mysql command line, the query succeeds. Does anyone know what is  
going on here? Is this a bug? Thanks for the help. Here’s the whole  
error. I know its really long, but just search for the | and you’ll see  
what I mean. Thanks again.

John

ActiveRecord::StatementInvalid (Mysql::Error: You have an error in your  
SQL synt ax; check the manual that corresponds to your MySQL  
server version for the right syntax to use near ‘OUTER JOIN  
mechanisms ON mechanisms.id = grants.mechanism\_i d WHERE ( (  
title LI’ at line 1: SELECT grants.`id` AS t0\_r0, grants.`brown_gra nt_identifier` AS t0\_r1, grants.`account` AS t0\_r2,  
grants.`grant_type_id` AS t0 \_r3, grants.`mechanism_id` AS  
t0\_r4, grants.`sponsor_agency_id` AS t0\_r5, grants  
.`award_year_indicator_id` AS t0\_r6, grants.`grant_home_id` AS t0\_r7,  
grants.`sp onsor` AS t0\_r8, grants.`fund_source` AS t0\_r9,  
grants.`department_name` AS t0\_r 10,  
grants.`start_date_overall` AS t0\_r11, grants.`end_date_overall` AS  
t0\_r12, grants.`start_date_current` AS t0\_r13,  
grants.`end_date_current` AS t0\_r14, gran ts.`total_amount` AS  
t0\_r15, grants.`direct_amount` AS t0\_r16, grants.`indirect_ amount` AS t0\_r17, grants.`total_amount_cfy` AS t0\_r18,  
grants.`direct_amount_cf y` AS t0\_r19,  
grants.`indirect_amount_cfy` AS t0\_r20, grants.`title` AS t0\_r21,  
grants.`abstract` AS t0\_r22, grants.`created_at` AS t0\_r23,  
grants.`created_on` AS t0\_r24, grants.`updated_at` AS t0\_r25,  
grants.`updated_on` AS t0\_r26, grants. `lock_version` AS  
t0\_r27, grants.`created_by` AS t0\_r28, grants.`updated_by` AS  
t0\_r29, sponsor\_agencies.`id` AS t1\_r0, sponsor\_agencies.`name` AS  
t1\_r1, sponso r\_agencies.`address` AS t1\_r2,  
sponsor\_agencies.`phone` AS t1\_r3, sponsor\_agenci  
es.`contact_name` AS t1\_r4, sponsor\_agencies.`created_at` AS t1\_r5,  
sponsor\_agen cies.`created_on` AS t1\_r6,  
sponsor\_agencies.`updated_at` AS t1\_r7, sponsor\_agen  
cies.`updated_on` AS t1\_r8, sponsor\_agencies.`lock_version` AS t1\_r9,  
sponsor\_ag encies.`created_by` AS t1\_r10,  
sponsor\_agencies.`updated_by` AS t1\_r11, mechanis ms.`id` AS  
t2\_r0, mechanisms.`name` AS t2\_r1, mechanisms.`description` AS t2\_r2,  
mechanisms.`created_at` AS t2\_r3, mechanisms.`created_on` AS t2\_r4,  
mechanisms. `updated_at` AS t2\_r5, mechanisms.`updated_on` AS  
t2\_r6, mechanisms.`lock_versio n` AS t2\_r7,  
mechanisms.`created_by` AS t2\_r8, mechanisms.`updated_by` AS t2\_r9  
FROM grants LEFT OUTER JOIN sponsor\_agencies ON sponsor\_agencies.id =  
grants.sp onsor\_agency\_id | LEFT OUTER JOIN mechanisms ON  
mechanisms.id = grants.mechanism \_id WHERE ( ( title LIKE  
‘%rasoulpour%’ OR abstract LIKE ‘%rasoulpour%’ OR  
department\_name LIKE ‘%rasoulpour%’ OR fund\_source LIKE ‘%rasoulpour%’  
) ) )

---

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [April 10, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/random-in-join-statement-with-more-than-one-include/57338/2 "2006-04-10T22:27:10Z")

</div>

If no one else has seen this, can anyone imagine how I may have done  
this to myself?
