1

With rails 3.2, mongoid

I don't want full-text search, I just wanna search one field of the model.

E.g I have a "People" scaffold with just a name field.

And I want to have a search form. For instance, I search "peter", if there's an exact match(case insensitive) of the search term - "peter" in the database, then I want it immediately redirect to peter show page without listing further search results.

However, if there's no exact match, then suggested results(in the database) will be shown.

Please kindly advise.

shingara
  • 46,608
  • 11
  • 99
  • 105
The questioner
  • 724
  • 10
  • 21

1 Answers1

0

You need test if the exact match exist in first case and use after a regexp to have some possibilities of result

user = User.where(:name => params[:name])
if user
  redirect_to user_url(user)
  return
else
  @users = User.where(:name => /params[:name]/i)
end
shingara
  • 46,608
  • 11
  • 99
  • 105