Query problem with 'LIKE'

Hi. I’m trying to implement a basic search, but the LIKE clause in the
query dosen’t seem to work as I expected. The result is only found if
the exact phrase is typed in to the field.

Have I made some sort of error?

query = params[:title]
@results = Auction.find(:all, :conditions => [“title like ?”, query])

Any help would be appreciated. Thanks.

query = params[:title]
@results = Auction.find(:all, :conditions => [“title like ?”, query])

You have to include the ‘%’

@results = Auction.find(:all, :conditions => [“title like ?”, ‘%’ +
query + ‘%’])

that should work.

-S

Now getting:

can’t convert Array into String

app/controllers/pages_controller.rb:13:in +' app/controllers/pages_controller.rb:13:insearchresults’

On Mon, 2008-03-31 at 21:36 +0200, Dan S. wrote:

Now getting:

can’t convert Array into String

app/controllers/pages_controller.rb:13:in +' app/controllers/pages_controller.rb:13:insearchresults’


because query is not a string and you MUST use a string, not an array in
a ‘LIKE’ statement.

Craig

Ah, ok thanks. Got it working now!:slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs