Skip to content

Instantly share code, notes, and snippets.

@manusajith
Last active December 14, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manusajith/5125763 to your computer and use it in GitHub Desktop.
Save manusajith/5125763 to your computer and use it in GitHub Desktop.
In Rails, try() lets you call methods on an object without having to worry about the possibility of that object being nil and thus raising an exception
user_details = User.find(id).try(:user_details).try(:first_name)
#no need to worry if user_details is nill
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment