# Querying multiple rows from db in my simple search

**URL:** <https://www.ruby-forum.com/t/querying-multiple-rows-from-db-in-my-simple-search/173826>\
**Category:** Rails\
**Created:** [August 16, 2009, 3:22am UTC](https://www.ruby-forum.com/t/querying-multiple-rows-from-db-in-my-simple-search/173826 "2009-08-16T03:22:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![webstic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@webstic](https://www.ruby-forum.com/u/webstic)\
**Post date:** [August 16, 2009, 3:22am UTC](https://www.ruby-forum.com/t/querying-multiple-rows-from-db-in-my-simple-search/173826/1 "2009-08-16T03:22:21Z")

</div>

Any suggestion why im getting this message in my Simple Search ?

\*\* i want to search in multiple rows in my db.

thanks

SyntaxError in PostsController#index

/Users/webstic/Sites/weblog/app/models/post.rb:7: syntax error,  
unexpected tCONSTANT, expecting ‘]’  
…onditions =\> [‘body LIKE?’ AND ‘title LIKE?’, “%#{search}%”]…  
^  
/Users/webstic/Sites/weblog/app/models/post.rb:7: syntax error,  
unexpected ‘]’, expecting kEND  
… ‘title LIKE?’, “%#{search}%”], :order =\> ‘created\_at DESC’  
^

code @ models/posts.rb

def self.search(search, page)  
paginate :per\_page =\> 3, :page =\> page,  
:conditions =\> [‘body LIKE?’ AND ‘title LIKE?’,  
“%#{search}%”], :order =\> ‘created\_at DESC’  
end  
end

---

<div class="post-metadata">

**Author:** ![webstic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@webstic](https://www.ruby-forum.com/u/webstic)\
**Post date:** [August 16, 2009, 10:11am UTC](https://www.ruby-forum.com/t/querying-multiple-rows-from-db-in-my-simple-search/173826/2 "2009-08-16T10:11:52Z")

</div>

:conditions =\> [‘body LIKE?’ AND ‘title LIKE?’, “%#{search}%”]

Try fixing the condition clause to:

```
 :conditions => ['body LIKE ? AND title LIKE ?', "%#{search}%", "%#

```

{search}%" ]

You are passing an array containing the SQL “WHERE” clause in a  
template. Each ‘?’ is a placeholder for the variables following the  
initial string containing the condition. For every ‘?’ in the clause  
there has to be a corresponding variable to be inserted. Your original  
had two placeholders and only one variable. Placeholders show where  
the corresponding variables will be inserted. In your code the  
variable would have been inserted as a run-on to the “LIKE” which SQL  
would have complained about because it would have been a syntax error.

The ‘AND’ is a boolean in the SQL statement and has to be part of it.  
Ruby was getting confused by your original statement because the “AND”  
was outside the single-quotes and thought you were ANDing two strings.

On Aug 15, 6:22 pm, Philip G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![webstic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@webstic](https://www.ruby-forum.com/u/webstic)\
**Post date:** [August 16, 2009, 11:32pm UTC](https://www.ruby-forum.com/t/querying-multiple-rows-from-db-in-my-simple-search/173826/3 "2009-08-16T23:32:24Z")

</div>

g\_f wrote:

> :conditions =\> [‘body LIKE?’ AND ‘title LIKE?’, “%#{search}%”]
> 
> Try fixing the condition clause to:
> 
> ```
> :conditions => ['body LIKE ? AND title LIKE ?', "%#{search}%", "%#
> 
> ```
> 
> {search}%" ]
> 
> You are passing an array containing the SQL “WHERE” clause in a  
> template. Each ‘?’ is a placeholder for the variables following the  
> initial string containing the condition. For every ‘?’ in the clause  
> there has to be a corresponding variable to be inserted. Your original  
> had two placeholders and only one variable. Placeholders show where  
> the corresponding variables will be inserted. In your code the  
> variable would have been inserted as a run-on to the “LIKE” which SQL  
> would have complained about because it would have been a syntax error.
> 
> The ‘AND’ is a boolean in the SQL statement and has to be part of it.  
> Ruby was getting confused by your original statement because the “AND”  
> was outside the single-quotes and thought you were ANDing two strings.
> 
> On Aug 15, 6:22ï¿½pm, Philip G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

thats work for me! thanks

-philip.g
