如何将 Rails 3 中的记录锁定特定的时间?

发布于 2024-12-09 07:44:01 字数 250 浏览 1 评论 0原文

我想做的基本上是让用户获得记录上的锁定并在特定的时间内拥有它,以便他们可以对其进行更改,就像维基百科一样。假设维基百科文章给用户一个小时的时间来编辑它,然后其他用户才能编辑它。

我如何使用 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 技术交流群。

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

发布评论

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

评论(3

空宴 2024-12-16 07:44:01

下面是一个创建锁但不删除它们的示例。

我把这个留给你。

在本例中,锁会在一小时后过期,但为了完成应用程序,它们应该在成功更新帖子后自动删除。

工作示例

或阅读

相关提交

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

℉絮湮 2024-12-16 07:44:01

您可以使用 acts_as_lockable_by gem 来完成此操作。

想象一下,您有一个患者(ActiveRecord)类,只能由一个用户编辑,并且应该将其锁定到该用户,直到他决定释放它:

class Patient < ApplicationRecord
  acts_as_lockable_by :id, ttl: 30.seconds
end

然后您可以在控制器中执行此操作:

class PatientsController < ApplicationController
  def edit
    if patient.lock(current_user.id) 
      # It will be locked for 30 seconds for the current user
      # You will need to renew the lock by calling /patients/:id/renew_lock
    else
      # Could not lock the patient record which means it is already locked by another user
    end
  end

  def renew_lock
    if patient.renew_lock(current_user.id)
      # lock renewed return 200
    else
      # could not renew the lock, it might be already released
    end
  end

  private

  def patient
    @patient ||= Patient.find(params[:id])
  end
end

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:

class Patient < ApplicationRecord
  acts_as_lockable_by :id, ttl: 30.seconds
end

Then you can do this in your controller:

class PatientsController < ApplicationController
  def edit
    if patient.lock(current_user.id) 
      # It will be locked for 30 seconds for the current user
      # You will need to renew the lock by calling /patients/:id/renew_lock
    else
      # Could not lock the patient record which means it is already locked by another user
    end
  end

  def renew_lock
    if patient.renew_lock(current_user.id)
      # lock renewed return 200
    else
      # could not renew the lock, it might be already released
    end
  end

  private

  def patient
    @patient ||= Patient.find(params[:id])
  end
end
荒人说梦 2024-12-16 07:44:01

添加名为“editable_until”:datetime 的字段,并在创建记录时设置特定日期 (Time.now + 30.min fe)。只需查询该字段即可了解用户是否有权更新记录。

class Post << AR
  before_validation :set_editable_time

  validate :is_editable

  def editable?
    self.editable_until.nil? || self.editable_until >= Time.now
  end

protected
  def is_editable
    self.errors[:editable_until] << "cannot be edited anymore" unless editable?
  end

  def set_editable_time
    self.editable_until ||= Time.now + 30.min
  end
end

Post.create(:params....)
=> <Post, ID:1, :editable_until => "2011-10-13 15:00:00">

Post.first.editable?
=> true

sleep 1.hour

Post.first.editable?
=> false

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.

class Post << AR
  before_validation :set_editable_time

  validate :is_editable

  def editable?
    self.editable_until.nil? || self.editable_until >= Time.now
  end

protected
  def is_editable
    self.errors[:editable_until] << "cannot be edited anymore" unless editable?
  end

  def set_editable_time
    self.editable_until ||= Time.now + 30.min
  end
end

Post.create(:params....)
=> <Post, ID:1, :editable_until => "2011-10-13 15:00:00">

Post.first.editable?
=> true

sleep 1.hour

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