ActiveRecord:更改并保存模型内的对象状态
我有以下代码:
def incoming_acceptation(incoming_code)
if invite_code == incoming_code
accepted = true
self.save
true
else
false
end
end
但它不会更改并保存接受为 true,它仍保持以前的状态 false。
@i.incoming_acceptation(incoming_code) => true
@i.accepted => false
I have the following code:
def incoming_acceptation(incoming_code)
if invite_code == incoming_code
accepted = true
self.save
true
else
false
end
end
But it does not change and save accepted to true, it remains in the previous state, false.
@i.incoming_acceptation(incoming_code) => true
@i.accepted => false
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议:
update_attribute
将更改并保存该属性。还有update_attributes
(注意s
)接受哈希值以一次更改多个属性:注意:
update_attribute
和update_attributes
> 当更改和保存成功时,两者都会返回true
,就像您的示例一样。I recommend:
update_attribute
will change and save that attribute. There's alsoupdate_attributes
(notice thes
) that accepts Hash to change multiple attributes at once:Note:
update_attribute
andupdate_attributes
both returntrue
when the change and save were successful, just like in your example.