Forum: Ruby on Rails Exception not caught?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C728d93eb5205cf20cac0c2928b0ad62?d=identicon&s=25 Jeff Jones (rurounijones)
on 2006-01-18 13:31
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 "<p class='center'>No matches found</p>"
    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 :)

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

Jeff
2f46d76f0e5db4dc318b03be07ebaac4?d=identicon&s=25 Tom Ward (Guest)
on 2006-01-18 14:15
(Received via mailing list)
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 "<p class='center'>No matches found</p>"
  else
    render :partial=> "componentlog"
  end
end

Tom Ward
C728d93eb5205cf20cac0c2928b0ad62?d=identicon&s=25 Jeff Jones (rurounijones)
on 2006-01-18 14:26
Tom Ward 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 "<p class='center'>No matches found</p>"
>   else
>     render :partial=> "componentlog"
>   end
> end
>
> Tom Ward

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
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2006-01-18 14:40
(Received via mailing list)
On 18.1.2006, at 15.26, Jeff Jones wrote:

>>     render_text "<p class='center'>No matches found</p>"
>>   else
>>     render :partial=> "componentlog"
>>   end
>> end
>>
>> Tom Ward
>
> 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
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2006-01-18 14:40
(Received via mailing list)
On 18.1.2006, at 15.00, Jarkko Laine 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
C728d93eb5205cf20cac0c2928b0ad62?d=identicon&s=25 Jeff Jones (rurounijones)
on 2006-01-18 14:47
Jarkko Laine wrote:
> On 18.1.2006, at 15.00, Jarkko Laine 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 ;)
This topic is locked and can not be replied to.