If you want to access an object only if it’s present, you can use Rails’ Object#presence.
The API docs on presence have a good explanation:
This is handy for any representation of objects where blank is the same as not present at all. For example, this simplifies a common check for HTTP POST/query parameters:
state = params[:state] if params[:state].present? country = params[:country] if params[:country].present? region = state || country || 'US'
…becomes:
region = params[:state].presence || params[:country].presence || 'US'
Tip that I submitted
You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.