如果两个人同时提交编辑会发生什么
这可能是一个愚蠢的问题,但我想知道如果两个用户同时编辑一些数据然后同时单击“提交”会发生什么,我假设 Rails 一个接一个地处理请求,所以有人会收到一条错误消息,但这对吗?
谢谢
一旦一个人编辑了数据,我就不再希望它可以访问或可编辑,这是通过验证处理的
我也在我的模型中进行了此验证
def account_active
if self.active == true
return true
else
return false
end
end
如果验证通过,则活动是控制器内的布尔集
This might be a stupid questions but I wanted to know what happens if two users edit some data at once and then both click submit at the same time, I assumed Rails handled requests one after the other and so someone would get an error message but is this correct?
Thanks
Once one person has edited data I dont want it to be accessible or editable anymore, which is handled by validations
Ive got this validation in my model as well
def account_active
if self.active == true
return true
else
return false
end
end
Where active is a Boolean set within the controller if the validations pass
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他答案中提到的,最新的写入获胜。
您可能不认为这是一个问题,但由于没有悲观锁阻止两个用户同时打开相同的编辑表单,因此两个用户可能认为他们所做的更改将被保存。
有一种方法可以解决这个问题,即在模型上使用版本号或时间戳,系统可以用它来告诉“用户正在编辑版本 1,但现在有版本 2”,并防止第二个用户覆盖第一个用户的写入。
Ryan Bates 精彩的 Railscasts 系列在Railscast 59:乐观锁定中介绍了如何设置此功能的基础知识。
As has been mentioned in other answers, the latest write wins.
You might not think this is a problem but as there's no pessimistic lock preventing two users from having the same edit form open at once, both users may think the change they're making will be saved.
There is a way around this by using a version number or timestamp on your models that the system can use to tell "the user was editing version 1, but now there's version 2" and prevent the second user from overriding the first user's write.
Ryan Bates' awesome Railscasts series has covered the basics on how to set this up in Railscast 59: Optimistic Locking.
你的网络服务器守护进程会一个接一个地处理请求;最后处理的请求将成为最新的更新。除非您编写一些逻辑来处理此类情况,否则没有人会收到错误消息。
Your web server daemon would handle the requests one after the other; whichever request gets handled last becomes the newest update. Nobody would receive an error message unless you write some logic to handle such cases.
与所有涉及盲写的竞争条件一样,最后一个获胜,除非您采取措施改变这一点。
As with all race conditions involving blind writes, last one wins unless you take steps to change that.
您原来的问题已得到解答,但我将添加以下内容:
对于验证,您可以简单地执行以下操作,
Ruby 隐式返回方法的最后一行。
Your original question was answered, but I'll add this:
For the validation, you can simply do the following
Ruby implicitly returns the last line of the method.