Is there a method to use instead of model.find(id) that returns nil if nothing is found - meaning there is no id that matches? -- 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 -~----------~----~----~----~------~----~------~--~---
David Modernfossil wrote:> Is there a method to use instead of model.find(id) that returns nil if > nothing is found - meaning there is no id that matches?Model.find(:first) or Model.find(:all).last -- 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 -~----------~----~----~----~------~----~------~--~---
On Sun, 2007-16-09 at 02:53 +0200, David Modernfossil wrote:> Is there a method to use instead of model.find(id) that returns nil if > nothing is found - meaning there is no id that matches?Try: model.find_by_id(id) -- Rick rick.tessner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks, but this is not what I am looking for. There is a possibility that the record I am searching for has been destroyed. For example, user with id = 1 has been deleted, but they posted a comment. I still want to display the comment and I want to show the users name if they still exist. Is there a method that looks for that id and returns the appropriate one if found and returns nil if it doesn''t exist without crashing? Jeremy Woertink wrote:> David Modernfossil wrote: >> Is there a method to use instead of model.find(id) that returns nil if >> nothing is found - meaning there is no id that matches? > > > Model.find(:first) > > or Model.find(:all).last-- 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 -~----------~----~----~----~------~----~------~--~---
David Modernfossil wrote:> Is there a method to use instead of model.find(id) that returns nil if > nothing is found - meaning there is no id that matches?Model.find_by_id(1) -- 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 -~----------~----~----~----~------~----~------~--~---
Yep, just rescue from the exception if it occurs like this begin @m = Model.find(id) rescue ActiveRecord::RecordNotFound #do something here if you want to end if @m #do stuff with @m else #do stuff without @m end Hope this helps ya. David Modernfossil wrote:> Thanks, but this is not what I am looking for. There is a possibility > that the record I am searching for has been destroyed. For example, > user with id = 1 has been deleted, but they posted a comment. I still > want to display the comment and I want to show the users name if they > still exist. Is there a method that looks for that id and returns the > appropriate one if found and returns nil if it doesn''t exist without > crashing? > > > Jeremy Woertink wrote: > >> David Modernfossil wrote: >> >>> Is there a method to use instead of model.find(id) that returns nil if >>> nothing is found - meaning there is no id that matches? >>> >> Model.find(:first) >> >> or Model.find(:all).last >> > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Danny is correct as well, both will work depending on what you are trying to do. I had forgotten that find_by_* dynamic finders returned nil. William Pratt wrote:> Yep, just rescue from the exception if it occurs like this > > begin > @m = Model.find(id) > rescue ActiveRecord::RecordNotFound > #do something here if you want to > end > > if @m > #do stuff with @m > else > #do stuff without @m > end > > Hope this helps ya. > > David Modernfossil wrote: >> Thanks, but this is not what I am looking for. There is a possibility >> that the record I am searching for has been destroyed. For example, >> user with id = 1 has been deleted, but they posted a comment. I still >> want to display the comment and I want to show the users name if they >> still exist. Is there a method that looks for that id and returns the >> appropriate one if found and returns nil if it doesn''t exist without >> crashing? >> >> >> Jeremy Woertink wrote: >> >>> David Modernfossil wrote: >>> >>>> Is there a method to use instead of model.find(id) that returns nil if >>>> nothing is found - meaning there is no id that matches? >>>> >>> Model.find(:first) >>> >>> or Model.find(:all).last >>> >> >> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David Modernfossil wrote:> Is there a method to use instead of model.find(id) that returns nil if > nothing is found - meaning there is no id that matches?foo = Model.find(id) rescue nil --Al Evans -- 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 -~----------~----~----~----~------~----~------~--~---
David Modernfossil wrote:> Is there a method to use instead of model.find(id) that returns nil if > nothing is found - meaning there is no id that matches?The #find method will do what you ask if you call it the right way: User.find(1) # raises error if no user with ID 1 User.find(:first, :conditions => { :id => 1 }) # returns nil if no user with ID 1 User.find(:all, :conditions => { :id => 1 }) # returns empty array if no user with ID 1 And the dynamic finder does what you want too: User.find_by_id(1) # returns nil if no user with ID 1 User.find_all_by_id(1) # returns empty array if no user with ID 1 I''ve never been a fan of the kitchen-sink nature of the #find method. It seems like three different methods to me. -- Josh Susser http://blog.hasmanythrough.com -- 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 -~----------~----~----~----~------~----~------~--~---
Personally I end up using this option a good deal: User.find(:first, :options=>{:id=> 1}) When you know that you''re looking for just one instance it''s simply easier to test for nil than to test for an empty array. On Sep 16, 7:18 pm, Josh Susser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> David Modernfossil wrote: > > Is there a method to use instead of model.find(id) that returns nil if > > nothing is found - meaning there is no id that matches? > > The #find method will do what you ask if you call it the right way: > > User.find(1) # raises error if no user with ID 1 > User.find(:first, :conditions => { :id => 1 }) # returns nil if no user > with ID 1 > User.find(:all, :conditions => { :id => 1 }) # returns empty array if > no user with ID 1 > > And the dynamic finder does what you want too: > > User.find_by_id(1) # returns nil if no user with ID 1 > User.find_all_by_id(1) # returns empty array if no user with ID 1 > > I''ve never been a fan of the kitchen-sink nature of the #find method. It > seems like three different methods to me. > > -- > Josh Susserhttp://blog.hasmanythrough.com > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Danny Burkes wrote in post #554031:> David Modernfossil wrote: >> Is there a method to use instead of model.find(id) that returns nil if >> nothing is found - meaning there is no id that matches? > > Model.find_by_id(1)Worked perfectly for me! :D Thanks! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.