これは、 acts_as_lockable_bygemを使用して行うことができます。
1人のユーザーのみが編集できる患者(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