views:

35

answers:

2
A: 

You have to handle the error using a rescue block:

def search
  @users = User.find_using_term params[:query]
rescue ActiveSearch::EmptyTermError
  flash[:notice] = "No Results Found"
  redirect_to :action => "home"
end

That way, though, if the search ran fine, but the result set is still empty, you won't be redirected. Here's some code that flashes and redirects you for both empty results and an EmptyTermError.

def search
  @users = User.find_using_term params[:query]
  return unless @users.empty?
rescue ActiveSearch::EmptyTermError
ensure
  flash[:notice] = "No Results Found"
  redirect_to :action => "home"
end

The return just leaves if we've got results.

The rescue block is empty, it just lets the error pass through.

And the ensure block runs whether we've handled an exception or not. (But certainly not if we returned before it.)

kch
A: 

There is a distinction between User.find() vs User.find_all()

The first one will throw an exception if no records are found. The second one will return nil. IN case of the first one it is like you are searching by 'primary key'.

May be related to your issue...

sujee
find_using_term is a method from the ActiveSearch plugin, it's not a dynamic finder.
kch