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.
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.
if the record is not found the find() throws an exception.
2012/6/19 John M. [email protected]
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
–
Fernando A.
ok I thought it would return nil
thanks for response
On Jun 19, 8:33pm, Fernando A. [email protected]
On 20 June 2012 01:55, John M. [email protected] wrote:
ok I thought it would return nil
It would take 30seconds to try it out in a console…
On Tuesday, 19 June 2012 20:29:25 UTC-4, John M. 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 J.
Alson you could just use where:
@user = User.where(:id => params[:user_id]).first
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs