Why does unsuccesful find throw an exception?

When querying my DB via find such as in:

@user = User.find(:first, :conditions => etc)

why do I get an exception when the find is unsuccessful? I thought the
regular behavior would be to just set @user to nil. can I change the way
this operates?

On 9/8/06, Mike K. removed_email_address[email protected] wrote:

When querying my DB via find such as in:

@user = User.find(:first, :conditions => etc)

why do I get an exception when the find is unsuccessful? I thought the
regular behavior would be to just set @user to nil. can I change the way
this operates?

Wow, you shouldn’t be getting an exception, at least not with :first.
The agile book specifically points this out! (First edition, anyway;
page 219 sidebar for those following at home.)

Find’ing with an id SHOULD throw an exception though; are you
specifying id in your conditions?

Actually it was not a :first it was a find by ID with some conditions
and the conditions weren’t met although the id was present.

But I’m still not clear. Should I have an exception handler for this
condition to take control? Or should I never search unless I know it
should be succesful?

On 9/8/06, Mike K. [email protected] wrote:

But I’m still not clear. Should I have an exception handler for this
condition to take control? Or should I never search unless I know it
should be succesful?

Well if your code is throwing an exception then yeah, you should
handle exceptions :wink:

How about a simple:

begin
@user = User.find(:first)
rescue
flash[:notice] = ‘Sorry, fresh out of users!’
redirect_to index_url
end

That is, of course, assuming the User.find(:first) call would throw an
exception if there are no users. I know it does for certain if you
specify in an invalid user though…

Thank you sir. I have gone and gotten a lot smarter about Ruby and
exceptions. I’m very familiar with this as an error-handling mechanism,
I just did not realize Ruby in general had such a robust architecture in
this regard. Since my first encounter with Ruby has been with Rails I am
finding more things to be impressed about the Ruby architecture the
further I go. I should also read the Find documentation a bit closer :wink:

On 9/8/06, Mike K. [email protected] wrote:

But I’m still not clear. Should I have an exception handler for this
condition to take control? Or should I never search unless I know it
should be succesful?

If you’re finding by id, then yes, you should be handling exceptions.
If you’re not finding by id, then if nothing’s there, you should
handle that eventuality accordingly.

So say the docs:
(http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000860)

* Find by id: This can either be a specific id (1), a list of ids

(1, 5, 6), or an array of ids ([5, 6, 10]). If no record can be found
for all of the listed ids, then RecordNotFound will be raised.
* Find first: This will return the first record matched by the
options used. These options can either be specific conditions or
merely an order. If no record can matched, nil is returned.
* Find all: This will return all the records matched by the
options used. If no records are found, an empty array is returned.