Rails 模型变量意外变化
在这里要发疯了。如有指点,不胜感激!
我有一个交付模型,我正在尝试添加一种方法来根据交付线路更新交付状态。该函数是在模型类中定义的,delivery_state 是模型属性之一:
def updateDeliveryState
expectedLines = DeliveryLine.where( :delivery_id => id,
:line_state => 'EXPECTED' )
logger.debug "State1: #{delivery_state}"
if expectedLines.length == 0
if delivery_state == 'EXPECTED' || delivery_state == 'RECEIVING'
delivery_state = 'RECEIVED' # commenting this line fixes it
save
end
else
logger.debug "State2: #{delivery_state}"
if delivery_state == 'EXPECTED'
logger.debug "Updating to receiving"
delivery_state = 'RECEIVING'
save
end
end
end
我在日志中看到的是,在 2 logger.debug
行之间,delivery_state 已被清除:
State1: EXPECTED
DeliveryLine Load (4.5ms) SELECT "delivery_lines".* FROM "delivery_lines"
WHERE "delivery_lines"."line_state" = 'EXPECTED'
AND "delivery_lines"."delivery_id" = 227
State2:
如果我注释掉上面代码中标记的行,它似乎工作正常:
State1: EXPECTED
DeliveryLine Load (9.6ms) SELECT "delivery_lines".* FROM "delivery_lines"
WHERE "delivery_lines"."line_state" = 'EXPECTED'
AND "delivery_lines"."delivery_id" = 227
State2: EXPECTED
Updating to receiving
但是,刷新后我可以看到在此之后交付仍然是预期的?
Going mad here. Any pointers gratefully received!
I've got a Delivery model and I'm trying to add a method to update the delivery state, based on delivery lines. This function is defined within the model class, and delivery_state is one of the model attributes:
def updateDeliveryState
expectedLines = DeliveryLine.where( :delivery_id => id,
:line_state => 'EXPECTED' )
logger.debug "State1: #{delivery_state}"
if expectedLines.length == 0
if delivery_state == 'EXPECTED' || delivery_state == 'RECEIVING'
delivery_state = 'RECEIVED' # commenting this line fixes it
save
end
else
logger.debug "State2: #{delivery_state}"
if delivery_state == 'EXPECTED'
logger.debug "Updating to receiving"
delivery_state = 'RECEIVING'
save
end
end
end
What I'm seeing in the log is that between the 2 logger.debug
lines, the delivery_state has been cleared:
State1: EXPECTED
DeliveryLine Load (4.5ms) SELECT "delivery_lines".* FROM "delivery_lines"
WHERE "delivery_lines"."line_state" = 'EXPECTED'
AND "delivery_lines"."delivery_id" = 227
State2:
If I comment out the line marked in the above code, it appears to work OK:
State1: EXPECTED
DeliveryLine Load (9.6ms) SELECT "delivery_lines".* FROM "delivery_lines"
WHERE "delivery_lines"."line_state" = 'EXPECTED'
AND "delivery_lines"."delivery_id" = 227
State2: EXPECTED
Updating to receiving
However, I can see after refreshing that the delivery is still EXPECTED after this??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
详细说明我的评论:看起来您正在
if
中创建一个局部变量。看:现在让我们确保调用设置器:
请参阅:为什么红宝石镶工需要“自我”。班级内的资格?
To elaborate on my comment: it seems like you are creating a local variable in the
if
. Look:Now let's make sure we call the setter:
See: Why do ruby setters need “self.” qualification within the class?
您可以尝试使用
save(:validate => false)
吗?有时 Rails 有这种令人讨厌的习惯,即默默地使验证失败并且不保存。
无论如何,你可能都想保存你的状态......:)
can you try to use
save(:validate => false)
?Sometimes Rails has this nasty habit of silently failing validations and not saving.
You probably want to save your state no matter what... :)