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 -- Posted via http://www.ruby-forum.com/.
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 On 1/18/06, Jeff Jones <rurounijones@hotmail.com> wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 WardAh 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 -- Posted via http://www.ruby-forum.com/.
On 18.1.2006, at 15.26, Jeff Jones wrote:> 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.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 Laine http://jlaine.net http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2363 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060118/cb218ee9/smime.bin
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 -- Jarkko Laine http://jlaine.net http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2363 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060118/73face91/smime.bin
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. > > //jarkkoOk 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 ;) -- Posted via http://www.ruby-forum.com/.