您可以使用acts_as_lockable_by gem 来做到这一点。
按照您问题中的示例:
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