About fuzzy search ....

Hi! I want to make a search box. I tried search box by using tag_name but it didn’t work.

posts_controller.rb

 @tag =   Tag.find_all_by_tag_name(params[:search_string])

is work fine.But It only return all matched words.

I tried below their code to be able to fuzzy search.

But they didn’t work.

@tag = Tag.find_all_by_tag_name(’%’ +params[:search_string]+’%’)

@tag = Tag.find(:all,:conditions => [‘tag_name LIKE ?’,"%{:search_string}%"])

Please help me some advise! thanks.

Hi! I want to make a search box. I tried search box by using tag_name but it didn't work.

# posts_controller.rb

     @tag = Tag.find_all_by_tag_name(params[:search_string])

is work fine.But It only return all matched words.

I tried below their code to be able to fuzzy search.

But they didn't work.

  @tag = Tag.find_all_by_tag_name('%' +params[:search_string]+'%')

  @tag = Tag.find(:all,:conditions => ['tag_name LIKE ?',"%{:search_string}%"])

Try this:

  @tag = Tag.find(:all,conditions => ['tag_name LIKE "%?%"',params[:search_string]]

Walter

Hi! I want to make a search box. I tried search box by using tag_name but
it didn't work.

# posts_controller.rb

 @tag =   Tag\.find\_all\_by\_tag\_name\(params\[:search\_string\]\)

is work fine.But It only return all matched words.

I tried below their code to be able to fuzzy search.

But they didn't work.

@tag = Tag.find_all_by_tag_name('%' +params[:search_string]+'%')

@tag = Tag.find(:all,:conditions => ['tag_name LIKE
?',"%{:search_string}%"])

You forgot the # and the params[]. Should be possibly
@tag = Tag.find(:all,:conditions => ['tag_name LIKE
?',"%#{params[:search_string]}%"])

If still not working look in the log and you will see the sql
generated which should give you a clue as to the problem.

Colin

To Colin

Thanks! It worked fine!
I struggled for this problem for a long time.

OK, Walter's suggestion should have worked also. If you had looked in
the log you would have seen the sql and been able to work it out
yourself probably. Have you had a look there to see what it shows so
that next time you have a problem you can look there?

Colin

I see. :slight_smile:

Tag Load (0.4ms) SELECT “tags”.* FROM “tags” WHERE (tag_name LIKE ‘%thanks%’)
Rendered posts/find.html.erb within layouts/application (1.9ms)
Completed 200 OK in 15ms (Views: 6.3ms | ActiveRecord: 0.4ms)