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…)
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.
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.
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.
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.