如何将 Rails 3 中的记录锁定特定的时间?
我想做的基本上是让用户获得记录上的锁定并在特定的时间内拥有它,以便他们可以对其进行更改,就像维基百科一样。假设维基百科文章给用户一个小时的时间来编辑它,然后其他用户才能编辑它。
我如何使用 Rails 3 实现这一目标?我已经阅读并发现悲观锁定是我应该使用的锁。鉴于此...我将使用什么样的机制在一小时后释放锁?
我的堆栈是 Rails 3、Heroku、PostgreSQL。
感谢您的任何答案,我喜欢看到代码,如果可以的话那就太棒了!
What I want to do is basically have a user obtain the lock on a record and have it for a specific amount of time so they can make changes to it, like wikipedia. So lets say a wikipedia article gives the user an hour to edit it before other users may edit it.
How could I achieve that with Rails 3? I have read up and found that pessimistic locking is what I should use for the lock. Given that... What kind of mechanism would I use for releasing the lock say after an hour?
My stack is Rails 3, Heroku, PostgreSQL.
Thanks for any answers and I love to see code if you can that would be so awesome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一个创建锁但不删除它们的示例。
我把这个留给你。
在本例中,锁会在一小时后过期,但为了完成应用程序,它们应该在成功更新帖子后自动删除。
工作示例
或阅读
相关提交
Here's an example that creates locks, but doesn't delete them.
I leave that up to you.
The locks do expire after an hour in this example, but to complete the app they should automatically be deleted on a successful update of a post.
working example
or read the
relevant commit
您可以使用 acts_as_lockable_by gem 来完成此操作。
想象一下,您有一个患者(ActiveRecord)类,只能由一个用户编辑,并且应该将其锁定到该用户,直到他决定释放它:
然后您可以在控制器中执行此操作:
You can do this with acts_as_lockable_by gem.
Imagine you have a patient (ActiveRecord) class that can only be edited by one user and it should be locked to this user till he decides to release it:
Then you can do this in your controller:
添加名为“editable_until”:datetime 的字段,并在创建记录时设置特定日期 (Time.now + 30.min fe)。只需查询该字段即可了解用户是否有权更新记录。
Add a field called "editable_until":datetime and set a specific date (Time.now + 30.min f.e.) when creating your record. And simply query this field to find out if the user has the right to update the record or not.