我应该怎么写:
if @parent.child.grand_child.attribute.present?
do_something
没有繁琐的 nil 检查来避免异常:
if @parent.child.present? && @parent.child.grandchild.present? && @parent.child.grandchild.attribute.present?
谢谢。
最佳答案
Rails 有 object.try(:method)
:
if @parent.try(:child).try(:grand_child).try(:attribute).present?
do_something
关于ruby-on-rails - 简化 Rails 中的多个 nil 检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8721949/