How should I write:
if @parent.child.grand_child.attribute.present?
do_something
without cumbersome nil checkings to avoid exception:
If the attribute you are checking is always the same, create a method in @parent.
def attribute_present?
@parent.child.present? && @parent.child.grandchild.present? && @parent.child.grandchild.attribute.present?
end
Alternatively, create has_many :through relationship so that @parent can get to grandchild so that you can use :
@parent.grandchild.try(:attribute).try(:present?)
Note: present? is not just for nil, it also checks for blank values, ''. You can just do @parent.grandchild.attribute if it's just nil checking