如果两个人同时提交编辑会发生什么

发布于 2024-09-06 01:27:31 字数 331 浏览 2 评论 0原文

这可能是一个愚蠢的问题,但我想知道如果两个用户同时编辑一些数据然后同时单击“提交”会发生什么,我假设 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜夜流光相皎洁 2024-09-13 01:27:31

正如其他答案中提到的,最新的写入获胜。

您可能不认为这是一个问题,但由于没有悲观锁阻止两个用户同时打开相同的编辑表单,因此两个用户可能认为他们所做的更改将被保存。

有一种方法可以解决这个问题,即在模型上使用版本号或时间戳,系统可以用它来告诉“用户正在编辑版本 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.

殊姿 2024-09-13 01:27:31

你的网络服务器守护进程会一个接一个地处理请求;最后处理的请求将成为最新的更新。除非您编写一些逻辑来处理此类情况,否则没有人会收到错误消息。

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.

玻璃人 2024-09-13 01:27:31

与所有涉及盲写的竞争条件一样,最后一个获胜,除非您采取措施改变这一点。

As with all race conditions involving blind writes, last one wins unless you take steps to change that.

挽清梦 2024-09-13 01:27:31

您原来的问题已得到解答,但我将添加以下内容:

对于验证,您可以简单地执行以下操作,

def account_active
  self.active?
end

Ruby 隐式返回方法的最后一行。

Your original question was answered, but I'll add this:

For the validation, you can simply do the following

def account_active
  self.active?
end

Ruby implicitly returns the last line of the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文