How would I avoid nil checks using Object.try for the following?
<%= image_tag(PeriodState.where("sla_id = ?", sla.id).last.state.image_file) %>
I’ve tried .try many different ways, but still receive errors, so my syntax is off. Thanks in advance!
tryisn’t really appropriate for this: whatever the outcomeimage_tagwill always be called – so you might end up calling it with nil. You need to check whether the image exists first then create an image tag only in this case. So I would get the PeriodState in your controller and have a simpleifin your view:Of course this won’t work if either
stateorimage_filecould also be nil.