There is an extremely useful .try()rails method that helps me a lot in such functions:
.try()
hash.try(:a).try(:b) # equal to # if hash.present? && hash.a.present? # hash.a.b # else # nil # end
Is there something similar in coffeescript?
Yes:
hash?.a?.b
See the existential operatorin the document
the existential operator