# ActiveRecord::StatementInvalid error

**URL:** <https://www.ruby-forum.com/t/activerecord-statementinvalid-error/145597>\
**Category:** Rails\
**Created:** [August 11, 2008, 11:16pm UTC](https://www.ruby-forum.com/t/activerecord-statementinvalid-error/145597 "2008-08-11T23:16:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 11, 2008, 11:16pm UTC](https://www.ruby-forum.com/t/activerecord-statementinvalid-error/145597/1 "2008-08-11T23:16:56Z")

</div>

I updated to Rails 2.1.0 and I’m getting an  
ActiveRecord::StatementInvalid error on an sql query that was working on  
the previous version. A simple call with and include getting data from 2  
tables. I’m not sure why this is happening now. Does anyone know what  
might cause this? Thanks in advance.

@furnii\_from\_furnii = Furni.find :all, :include =\> “raider”,  
:conditions =\> “”+Furni.conditions\_by\_like(@search)+" OR "  
+Raider.conditions\_by\_like(@search) + “”

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 12, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/activerecord-statementinvalid-error/145597/2 "2008-08-12T21:52:42Z")

</div>

On 11 Aug 2008, at 22:16, Sam G. wrote:

> I updated to Rails 2.1.0 and I’m getting an  
> ActiveRecord::StatementInvalid error on an sql query that was  
> working on  
> the previous version. A simple call with and include getting data  
> from 2  
> tables. I’m not sure why this is happening now. Does anyone know what  
> might cause this? Thanks in advance.

short answer: qualify your conditions on included tables (ie  
raiders.foo = ‘bar’ instead of foo=‘bar’)  
long answer:

> **[Mixing :include and :conditions - Space Vatican](https://www.spacevatican.org/2008/4/29/include-and-conditions/)**
>
> We all love :include, it cuts down on the number of queries made to the database (especially when iterating over a collection) and generally makes …

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 12, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/activerecord-statementinvalid-error/145597/3 "2008-08-12T22:08:04Z")

</div>

Frederick C. wrote:

> On 11 Aug 2008, at 22:16, Sam G. wrote:
> 
> > I updated to Rails 2.1.0 and I’m getting an  
> > ActiveRecord::StatementInvalid error on an sql query that was  
> > working on  
> > the previous version. A simple call with and include getting data  
> > from 2  
> > tables. I’m not sure why this is happening now. Does anyone know what  
> > might cause this? Thanks in advance.
> 
> short answer: qualify your conditions on included tables (ie  
> raiders.foo = ‘bar’ instead of foo=‘bar’)  
> long answer:  
> [Mixing :include and :conditions - Space Vatican](http://www.spacevatican.org/2008/4/29/include-and-conditions)

I’m not sure how to, since I’m using a function that loops through the  
columns. How do I include the tables in it? see below.

def conditions\_by\_like(value, \*columns)  
myArray = value.gsub(/[^\A-Za-z0-9\s]+/, “”).split(/ /)  
myArray.collect do |itemz|  
columns = self.user\_columns if columns.size==0  
columns = columns[0] if columns[0].kind\_of?(Array)  
conditions = columns.map { |c|  
c = c.name if c.kind\_of? ActiveRecord::ConnectionAdapters::Column  
“`#{c}` LIKE " +  
ActiveRecord::Base.connection.quote(”%#{itemz}%“)  
}  
end.join(” OR ")  
end

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 12, 2008, 10:24pm UTC](https://www.ruby-forum.com/t/activerecord-statementinvalid-error/145597/4 "2008-08-12T22:24:23Z")

</div>

On 12 Aug 2008, at 21:08, Sam G. wrote:

> > > tables. I’m not sure why this is happening now. Does anyone know  
> > > I’m not sure how to, since I’m using a function that loops through the  
> > > columns. How do I include the tables in it? see below.

instead of your query saying  
thing LIKE ‘bar’  
it should say  
table\_name.thing LIKE ‘bar’

You already know what the table name is (self.table\_name) so you just  
need to do it.

Fred

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 13, 2008, 8:35am UTC](https://www.ruby-forum.com/t/activerecord-statementinvalid-error/145597/5 "2008-08-13T08:35:32Z")

</div>

On 12 Aug 2008, at 22:20, Sam G. wrote:

> > thing LIKE ‘bar’  
> > an error. It does not make any sense since the column furnii.name does  
> > exist.

You’ve quoted it wrong: `furnii.name` means a column called  
furnii.name, as opposed to `furnii`.`name` which means a name column  
on the furnii tqable.

Fred

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 12, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/activerecord-statementinvalid-error/145597/6 "2008-08-12T23:20:23Z")

</div>

Frederick C. wrote:

> On 12 Aug 2008, at 21:08, Sam G. wrote:
> 
> > > > tables. I’m not sure why this is happening now. Does anyone know  
> > > > I’m not sure how to, since I’m using a function that loops through the  
> > > > columns. How do I include the tables in it? see below.
> 
> instead of your query saying  
> thing LIKE ‘bar’  
> it should say  
> table\_name.thing LIKE ‘bar’
> 
> You already know what the table name is (self.table\_name) so you just  
> need to do it.
> 
> Fred

I simplified the query below limiting it to the furnii and raiders  
tables for testing and did exactly what you told me, but I’m getting  
an error. It does not make any sense since the column furnii.name does  
exist.

Mysql::Error: #42S22Unknown column ‘furnii.name’ in ‘where clause’:  
SELECT `furnii`.`id` AS t0\_r0, `furnii`.`original_raider_id` AS t0\_r1,  
`furnii`.`name` AS t0\_r2, `furnii`.`description` AS t0\_r3,  
`furnii`.`security_id` AS t0\_r4, `furnii`.`published` AS t0\_r5,  
`furnii`.`created_at` AS t0\_r6, `furnii`.`updated_at` AS t0\_r7,  
`furnii`.`tmp` AS t0\_r8, `furnii`.`modulename` AS t0\_r9,  
`furnii`.`imageasset_id` AS t0\_r10, `raiders`.`id` AS t1\_r0,  
`raiders`.`login` AS t1\_r1, `raiders`.`email` AS t1\_r2,  
`raiders`.`crypted_password` AS t1\_r3, `raiders`.`salt` AS t1\_r4,  
`raiders`.`created_at` AS t1\_r5, `raiders`.`updated_at` AS t1\_r6,  
`raiders`.`remember_token` AS t1\_r7,  
`raiders`.`remember_token_expires_at` AS t1\_r8, `raiders`.`fname` AS  
t1\_r9, `raiders`.`lname` AS t1\_r10, `raiders`.`imageasset_id` AS t1\_r11,  
`raiders`.`super_user` AS t1\_r12, `raiders`.`gender` AS t1\_r13,  
`raiders`.`birthdate` AS t1\_r14, `raiders`.`postalcode` AS t1\_r15 FROM  
`furnii` LEFT OUTER JOIN `raiders` ON `raiders`.id =  
`furnii`.original\_raider\_id WHERE (`furnii.name` LIKE ‘%jack%’ OR  
`furnii.description` LIKE ‘%jack%’ OR `furnii.published` LIKE ‘%jack%’  
OR `furnii.tmp` LIKE ‘%jack%’ OR `furnii.modulename` LIKE ‘%jack%’ OR  
`raiders.login` LIKE ‘%jack%’ OR `raiders.email` LIKE ‘%jack%’ OR  
`raiders.crypted_password` LIKE ‘%jack%’ OR `raiders.salt` LIKE ‘%jack%’  
OR `raiders.remember_token` LIKE ‘%jack%’ OR `raiders.fname` LIKE  
‘%jack%’ OR `raiders.lname` LIKE ‘%jack%’ OR `raiders.super_user` LIKE  
‘%jack%’ OR `raiders.gender` LIKE ‘%jack%’ OR `raiders.birthdate` LIKE  
‘%jack%’ OR `raiders.postalcode` LIKE ‘%jack%’)

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [August 13, 2008, 1:03pm UTC](https://www.ruby-forum.com/t/activerecord-statementinvalid-error/145597/7 "2008-08-13T13:03:07Z")

</div>

Frederick C. wrote:

> On 12 Aug 2008, at 22:20, Sam G. wrote:
> 
> > > thing LIKE ‘bar’  
> > > an error. It does not make any sense since the column furnii.name does  
> > > exist.
> 
> You’ve quoted it wrong: `furnii.name` means a column called  
> furnii.name, as opposed to `furnii`.`name` which means a name column  
> on the furnii tqable.
> 
> Fred

got it. thanks again
