def self.current_transaction_batch_id
@current_transaction_batch_id ||= SecureRandom.hex
@current_transaction_batch_id
end
嗨,我有以下方法應該生成并回傳一個批處理 id,我需要對每個請求都是唯一的。但是,問題在于此方法通過多個請求回傳相同的值,這意味著變數@current_transaction_batch_id 通過請求持續存在。
提前感謝您的幫助
uj5u.com熱心網友回復:
亞歷克斯在評論中所說的是正確的:
該current_transaction_batch_id方法是一個類方法。通過在其中設定 instance_variable @current_transaction_batch_id,您將其設定在類上,而不是類的實體上。通過記憶它,您可以保持它不變。該類只加載一次并在請求之間保持,因此該值永遠不會改變。
您需要更改代碼,因此您正在處理實體,而不是類:
def current_transaction_batch_id
@current_transaction_batch_id ||= SecureRandom.hex
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/479272.html
