检查 activerecord 对象中多个属性的值存在

Check for value presence of multiple attributes in a activerecord object

我有一个 rails 模型对象,我在其中检查是否存在某些 select 属性值。

例如,假设人物模型有 fname、lname、age attributes.How 我可以单独检查 fname、lname 是否存在,而不是空白或 null,例如我知道我们可以做类似 person.fname.present? && person.lname.present? 但是否有更简洁和 ruby 友好的方式来做同样的事情?

您可以添加任何要检查的属性作为 values_at 函数的参数。

person.attributes.values_at(:fname, :lname).all?(&:present?)