ruby-on-rails – Rails – 混合’发送’和’尝试’

我有一个可能是零的对象.在下一行中,我根据某些条件动态获取对象的任何一个参数.我正在使用带有if else块的object.try().现在我想使用send()以便我可以实现一些有效的代码.

form_name = 'parent' ( received from argument )
mes_record = Mark.first ( this can be nil )

if x == condition_1
    mes_record.parent
elsif x == condition_2
    mes_record.brother
elsif x == condition_3
    mes_record.sister
else
    mes_record.father
end

现在我想避免使用if else梯子并使用send.

if mes_record.present?
      mes_record.send("#{form}")
 else
      mes_record = 'not available'
 end

我正在寻找类似尝试的东西.

mes_record.try("dynamically substitute the attribute name here.") || 'not available'

有什么帮助?!

解决方法

尝试了一个小时后,我找到了解决方案.

帽子到Rails尝试方法.

mes_record.try(:send,“#{form}”)|| ‘无法使用’

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...