如果条件与假从来没有工作ruby在轨道上

时间:2016-02-10 06:07:31

标签: ruby-on-rails activerecord

目前我正在if中将api条件的某些值更新为

motion_detection.frequency = frequency if frequency
motion_detection.minPosition = minPosition if minPosition
motion_detection.step = step if step
motion_detection.min = min if min
motion_detection.threshold = threshold if threshold
motion_detection.enabled = enabled

motion_detection.alert_email = alert_email if alert_email
motion_detection.alert_interval_min = alert_interval_min if alert_interval_min
motion_detection.sensitivity = sensitivity if sensitivity
motion_detection.x1 = x1 if x1
motion_detection.x2 = x2 if x2
motion_detection.y1 = y1 if y1
motion_detection.y2 = y2 if y2
motion_detection.schedule = JSON.parse(inputs["schedule"]) if schedule
motion_detection.save

motion_detection

如果我放motion_detection.enabled = enabled if enabled 那么一旦它TRUE,即使我放FALSE,它也永远不会回到enabled = false。为什么会这样?我想让它运行为假,因为我可能会启用false但它只适用于TRUE然后永远不会回到假

1 个答案:

答案 0 :(得分:0)

如果您只想在motion_detection.enabled = enabled提供value时才指定enabled ...

要做到这一点,你可以使用..

motion_detection.enabled = enabled unless enabled.nil?

motion_detection.enabled = enabled if !enabled.nil?

这样您就可以presence分配enabled而不是value ..

这是一个简单的红宝石小提琴来说明rubyfiddle