# Problem with :conditions in find

**URL:** <https://www.ruby-forum.com/t/problem-with-conditions-in-find/111204>\
**Category:** Rails\
**Created:** [August 3, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/problem-with-conditions-in-find/111204 "2007-08-03T20:02:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [August 3, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/problem-with-conditions-in-find/111204/1 "2007-08-03T20:02:43Z")

</div>

I’m having the strangest problem…

I’m getting syntax errors on this find…

@lob\_messages = ProviderMessage.find(:all,  
:conditions =\> [ “to = ?”,  
“GFA”],  
:order =\> ‘created\_at DESC’,  
:limit =\> 5)  
The Exception Rails is returning is:Mysql::Error: #42000You have an  
error in your SQL syntax; check the manual that corresponds to your  
MySQL server version for the right syntax to use near ‘to = ‘GFA’)  
ORDER BY created\_at DESC LIMIT 5’ at line 1: SELECT \* FROM  
provider\_messages WHERE (to = ‘GFA’) ORDER BY created\_at DESC LIMIT 5I  
really would appreciate another set of eyeballs. Anybody see anything  
wrong with the find?And I can’t get a find with :conditions =\> [  
“something IN (?)”, some\_array] to work either.Any assistance would  
really be appreciated!TIA,Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [August 3, 2007, 8:19pm UTC](https://www.ruby-forum.com/t/problem-with-conditions-in-find/111204/2 "2007-08-03T20:19:47Z")

</div>

On 8/4/07, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The Exception Rails is returning is:
> 
> Mysql::Error: #42000You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘to = ‘GFA’) ORDER BY created\_at DESC LIMIT 5’ at line 1: SELECT \* FROM provider\_messages WHERE (to = ‘GFA’) ORDER BY created\_at DESC LIMIT 5
> 
> I really would appreciate another set of eyeballs. Anybody see anything wrong with the find?

It seems “to” is a reserved word in mysql, check the manual yourself.

And I can’t get a find with :conditions =\> [ “something IN (?)”,  
some\_array] to work either.

> 

use some\_array.joins(“,”) instead.

Any assistance would really be appreciated!

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [August 3, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/problem-with-conditions-in-find/111204/3 "2007-08-03T22:42:05Z")

</div>

On Aug 3, 2007, at 2:18 PM, Ye Dingding wrote:

> The Exception Rails is returning is:  
> And I can’t get a find with :conditions =\> [ “something IN (?)”,  
> some\_array] to work either.
> 
> use some\_array.joins(“,”) instead.
> 
> Any assistance would really be appreciated!  
> TIA,  
> Bill  
> While I’d strongly suggest that you change the name of that column,  
> if you actually managed to create a column with that name, you should  
> be able to add ID quotes (that is, backticks or grave accents) to the  
> column name:

```
 :conditions => ["`to` = ?", "GFA"],

```

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [August 3, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/problem-with-conditions-in-find/111204/4 "2007-08-03T20:26:49Z")

</div>

Ye Dingding wrote:

> It seems “to” is a reserved word in mysql,

Thanks. It never occurred to me that it might be a reserved word  
problem.

> use some\_array.joins(",") instead.

And thanks again!

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [August 4, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/problem-with-conditions-in-find/111204/5 "2007-08-04T14:01:19Z")

</div>

Rob,  
Would you mind taking this one step further and show how I’d write the  
condition to find an integer instead of a string.  
I get the same error message described above when I try to make  
the :condition for an integer like this;  
:conditions =\> [‘intfieldvalue = ?, intvalue’]  
surprisingly, it passes with this, but the condition is disregared for  
some reason?  
:conditions =\> [‘intfieldvalue = intvalue’]  
I am grateful for a reply.  
Kathy  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [August 4, 2007, 12:19am UTC](https://www.ruby-forum.com/t/problem-with-conditions-in-find/111204/6 "2007-08-04T00:19:19Z")

</div>

Hi Rob,

Rob B. wrote:

> While I’d strongly suggest that you change the name  
> of that column,

Did that.

> if you actually managed to create a column with that name,

Did. Via a migration.

> you should be able to add ID quotes (that is, backticks or  
> grave accents) to the column name:

Thanks for that information. When I get some time, I’ll go back and see  
if  
that works.

Thanks again!  
Bill  
:conditions =\> [“`to` = ?”, “GFA”],

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [August 4, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/problem-with-conditions-in-find/111204/7 "2007-08-04T14:06:20Z")

</div>

Hi –

On Sat, 4 Aug 2007, removed\_email\_address@domain.invalid wrote:

> Rob,  
> Would you mind taking this one step further and show how I’d write the  
> condition to find an integer instead of a string.  
> I get the same error message described above when I try to make  
> the :condition for an integer like this;  
> :conditions =\> [‘intfieldvalue = ?, intvalue’]

That has to be:

:conditions =\> [‘intfieldvalue = ?’, intvalue]

The values to be interpolated have to be outside the string, as  
separate array elements.

> surprisingly, it passes with this, but the condition is disregared for  
> some reason?  
> :conditions =\> [‘intfieldvalue = intvalue’]

If you want it all in one string (usually not advisable, for security  
reasons), you’d have to do Ruby-style string interpolation:

:conditions =\> “intfieldvalue = #{intvalue}”

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
