检查 Rails 中所有子对象的布尔属性
我有一个有多个参与者的会议模型。参与者有一些布尔属性:接受、拒绝等。我想检查会议的所有参与者是否都被接受== true。 是否有一种类似于 sum 方法的快速方法来检查子对象的所有布尔属性(如total_price = items.sum(&:price) )?
I have a Meeting model which has multiple Participants. Participant has a few boolean attributes: accepted, rejected ect. I would like check all participants of a meeting if their are all accepted == true.
Is there a quick way to check all boolean attributes of child objects similar to sum method (as in total_price = items.sum(&:price) )?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
count
:如果你的布尔列中没有 NULL 需要担心:
或者你可以这样做(如 klochner)来解决常见的 NULL 问题并避免双重
计数
:您可以检查几个布尔列也立刻。
You could use
count
:And if you don't have NULLs to worry about in your boolean column:
Or you could do it this way (as suggested by klochner) to get around the usual NULL problems and avoid a double
count
:You could check several boolean columns at once too.
看一下可枚举模块
Take a look at the enumerable module