Assuming I have a model named "Person" & that person is defined by name, age, gender & race.
假设我有一个名为“人”的模型,那个人是根据姓名,年龄,性别和种族来定义的。
How do I iterate through the model to find out which values are nil?
如何遍历模型以找出哪些值为零?
For example:
name: peter
age: 34
gender: nil
race: nil
--Nil count: 2--
- 尼尔数:2--
I understand I would have iterate through each field, do a +1 if value if nil & output the total value.
我知道我会遍历每个字段,如果为n,则输入+1 if值并输出总值。
Thanks for any help or guidance!
感谢您的帮助或指导!
1 个解决方案
#1
9
If your instance is p
then:
如果您的实例是p,那么:
nils = p.attributes.values.select(&:nil?).count
will give you the number of nil
attributes.
会给你nil属性的数量。
#1
9
If your instance is p
then:
如果您的实例是p,那么:
nils = p.attributes.values.select(&:nil?).count
will give you the number of nil
attributes.
会给你nil属性的数量。