Exception not caught?

Hello all.

I have started reading up on exceptions and tried replacing a manual
check with one. Given the following code:

def results
begin
@componentlogs = Componentlog.find(:all, :conditions => [
“cl_compname = ?”, params[:componentlog][:cl_compname].strip] )
rescue ActiveRecord::RecordNotFound
render_text “

No matches found


else
render :partial=> “componentlog”
end
end

If I searched for a nonexistant record I would expect the exception to
be raised and caught. What is actually happening I am not sure. All I
know is that it tries to render the partial (which falls over because
there is no data because no record was found). Have I managed to
misunderstand exceptions totally?

Any insight would be much appreciated :slight_smile:

(I should keep a count of the number of times a problem of mine has been
fixed here…)

Jeff

No exception will be thrown using find :all, instead an empty array
will be returned if there are no matches. So the code could be
written:

def results
@componentlogs = Componentlog.find(:all, :conditions =>
[“cl_compname = ?”, params[:componentlog][:cl_compname].strip] )

if @componentlogs.empty?
render_text “

No matches found


else
render :partial=> “componentlog”
end
end

Tom W.

Tom W. wrote:

No exception will be thrown using find :all, instead an empty array
will be returned if there are no matches. So the code could be
written:

def results
@componentlogs = Componentlog.find(:all, :conditions =>
[“cl_compname = ?”, params[:componentlog][:cl_compname].strip] )

if @componentlogs.empty?
render_text “

No matches found


else
render :partial=> “componentlog”
end
end

Tom W.

Ah nuts. I had an inkling that that may have been the problem but I
wanted to be sure. Is there a good technical reason for this behaviour
because it seems counter-intuitive to me.

Thanks

Jeff

On 18.1.2006, at 15.00, Jarkko L. wrote:

Using find without :all is meant for situations when you “know”
that the entry with a given id exists (like a page for a given blog
post). So if it doesn’t, something’s wrong and an exception is
about the right thing to throw at that point.

Oh, and you should also be “certain” that the query only returns one
item, because if it returns more, something’s wrong again. So use
find without :all primarily with ids like primary keys.

//jarkko

On 18.1.2006, at 15.26, Jeff J. wrote:

render_text "<p class='center'>No matches found</p>"

else
render :partial=> “componentlog”
end
end

Tom W.

Ah nuts. I had an inkling that that may have been the problem but I
wanted to be sure. Is there a good technical reason for this behaviour
because it seems counter-intuitive to me.

Using find without :all is meant for situations when you “know” that
the entry with a given id exists (like a page for a given blog post).
So if it doesn’t, something’s wrong and an exception is about the
right thing to throw at that point.

In contrast, you should be using :all in situations where you really
don’t know whether the query should return zero, one or more items.
That would be the case with your example.

//jarkko

Jarkko L. wrote:

On 18.1.2006, at 15.00, Jarkko L. wrote:

Using find without :all is meant for situations when you “know”
that the entry with a given id exists (like a page for a given blog
post). So if it doesn’t, something’s wrong and an exception is
about the right thing to throw at that point.

Oh, and you should also be “certain” that the query only returns one
item, because if it returns more, something’s wrong again. So use
find without :all primarily with ids like primary keys.

//jarkko

Ok yes that makes sense. Since with a find :all you would usually need
to check what was returned anyway (zilch, 1 or more). Are there any
other exceptions that find :all doesn’t return as opposed to find, I
can’t see a list of exceptions in the api documetation about that (It
lists em but doesn’t say what might / might not return them).

Many thanks all

(Times problem fixed = 2 :wink: