# ActiveRecord::StatementInvalid error

**URL:** <https://discuss.rubyonrails.org/t/activerecord-statementinvalid-error/33116>\
**Category:** rubyonrails-talk\
**Created:** [August 12, 2008, 7:51pm UTC](https://discuss.rubyonrails.org/t/activerecord-statementinvalid-error/33116 "2008-08-12T19:51:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 12, 2008, 7:51pm UTC](https://discuss.rubyonrails.org/t/activerecord-statementinvalid-error/33116/1 "2008-08-12T19:51:29Z")

</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.

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)

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 12, 2008, 8:08pm UTC](https://discuss.rubyonrails.org/t/activerecord-statementinvalid-error/33116/2 "2008-08-12T20:08:04Z")

</div>

Frederick Cheung 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) &nbsp;&nbsp;&nbsp;&nbsp;myArray = value.gsub(/[^\A-Za-z0-9\s]+/, "").split(/ /) &nbsp;&nbsp;&nbsp;&nbsp;myArray.collect do |itemz| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;columns = self.user\_columns if columns.size==0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;columns = columns[0] if columns[0].kind\_of?(Array) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conditions = columns.map { |c| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = c.name if c.kind\_of? ActiveRecord::ConnectionAdapters::Column &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"`#{c}` LIKE " + ActiveRecord::Base.connection.quote("%#{itemz}%") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end.join(" OR ") &nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 12, 2008, 8:23pm UTC](https://discuss.rubyonrails.org/t/activerecord-statementinvalid-error/33116/3 "2008-08-12T20:23:14Z")

</div>

> Frederick Cheung 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.

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:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 12, 2008, 9:20pm UTC](https://discuss.rubyonrails.org/t/activerecord-statementinvalid-error/33116/4 "2008-08-12T21:20:23Z")

</div>

Frederick Cheung 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:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 13, 2008, 6:34am UTC](https://discuss.rubyonrails.org/t/activerecord-statementinvalid-error/33116/5 "2008-08-13T06:34:47Z")

</div>

> Frederick Cheung 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.

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
