It''s a very basic question, i want just to test if this query did return any result. @BookDetails = Book.find(params[:id]) I tried "nil", but it seems that even if there isn''t any book with that id, @BookDetails is != nil. Help please! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Mahmou, Mahmou M''hiri wrote:> It''s a very basic question, i want just to test if this > query did return any result. > > @BookDetails = Book.find(params[:id]) > > I tried "nil", but it seems that even if there isn''t any > book with that id, @BookDetails is != nil.It should be nil if there''s no record in the Books table with that id. One thing you might want to do, though, is use: Book.find(params[:id].to_i) since params are always strings. Other than that, you''ll need to provide more info. hth, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bill Walton wrote:> Hi Mahmou, > > Mahmou M''hiri wrote: >> It''s a very basic question, i want just to test if this >> query did return any result. >> >> @BookDetails = Book.find(params[:id]) >> >> I tried "nil", but it seems that even if there isn''t any >> book with that id, @BookDetails is != nil. > > It should be nil if there''s no record in the Books table with that id. > One > thing you might want to do, though, is use: > Book.find(params[:id].to_i) since params are always strings. Other than > that, you''ll need to provide more info. > > hth, > BillStrings are no problem for ActiveRecord finders. You Book.find(some_id) should raise an exception if there is no record. And Book.find_by_id(some_id) should return nil if no record is found. Try this: begin Book.find(params[:id]) rescue ActiveRecord::RecordNotFound puts "Not Found!" end The rescue traps the RecordNotFound exception and lets you do something if it happens. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---