Hey all, I saw this piece of code: @user = User.find(params[:user_id]) rescue nil why rescue with a nil here? If the user is not found, it will be nil anyway. -- 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.
if the record is not found the find() throws an exception. 2012/6/19 John Merlino <stoicism1-YDxpq3io04c@public.gmane.org>> Hey all, > > I saw this piece of code: > > @user = User.find(params[:user_id]) rescue nil > > why rescue with a nil here? If the user is not found, it will be nil > anyway. > > -- > 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. > >-- Fernando Almeida www.fernandoalmeida.net -- 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.
http://api.rubyonrails.org/classes/ActiveRecord/RecordNotFound.html 2012/6/19 Fernando Almeida <fernando-7WGqr3rU1tV1NwFxuVVnt9HuzzzSOjJt@public.gmane.org>> if the record is not found the find() throws an exception. > > > 2012/6/19 John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> > >> Hey all, >> >> I saw this piece of code: >> >> @user = User.find(params[:user_id]) rescue nil >> >> why rescue with a nil here? If the user is not found, it will be nil >> anyway. >> >> -- >> 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. >> >> > > > -- > Fernando Almeida > www.fernandoalmeida.net > >-- Fernando Almeida www.fernandoalmeida.net -- 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.
ok I thought it would return nil thanks for response On Jun 19, 8:33 pm, Fernando Almeida <ferna...-7WGqr3rU1tV1NwFxuVVnt9HuzzzSOjJt@public.gmane.org> wrote:> if the record is not found the find() throws an exception. > > 2012/6/19 John Merlino <stoici...-YDxpq3io04c@public.gmane.org> > > > > > > > > > > > Hey all, > > > I saw this piece of code: > > > @user = User.find(params[:user_id]) rescue nil > > > why rescue with a nil here? If the user is not found, it will be nil > > anyway. > > > -- > > 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. > > -- > Fernando Almeidawww.fernandoalmeida.net-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Just a reminder: it''s just specific with the find(). find_by_* will return nil unless you add a ! (e,g,, find_by_name!()) then it also returns the exception On Wed, Jun 20, 2012 at 8:55 AM, John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> wrote:> ok I thought it would return nil > > thanks for response > > On Jun 19, 8:33 pm, Fernando Almeida <ferna...-7WGqr3rU1tV1NwFxuVVnt9HuzzzSOjJt@public.gmane.org> > wrote: > > if the record is not found the find() throws an exception. > > > > 2012/6/19 John Merlino <stoici...-YDxpq3io04c@public.gmane.org> > > > > > > > > > > > > > > > > > > > > > Hey all, > > > > > I saw this piece of code: > > > > > @user = User.find(params[:user_id]) rescue nil > > > > > why rescue with a nil here? If the user is not found, it will be nil > > > anyway. > > > > > -- > > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > > Fernando Almeidawww.fernandoalmeida.net > > -- > 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. > >-- 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.
On 20 June 2012 01:55, John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> wrote:> ok I thought it would return nilIt would take 30seconds to try it out in a console... -- 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.
Alson you could just use where: @user = User.where(:id => params[:user_id]).first On Wednesday, June 20, 2012 3:29:25 AM UTC+3, John Merlino wrote:> > Hey all, > > I saw this piece of code: > > @user = User.find(params[:user_id]) rescue nil > > why rescue with a nil here? If the user is not found, it will be nil > anyway.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/QRvpn4dYbzcJ. 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.
On Tuesday, 19 June 2012 20:29:25 UTC-4, John Merlino wrote:> > Hey all, > > I saw this piece of code: > > @user = User.find(params[:user_id]) rescue nil > > why rescue with a nil here?Because the author forgot about find_by_id, which would accomplish the exact same thing (returning nil if no record was found). --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/8gD2kFdxxOcJ. 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.