我正在开发一种跟踪包裹的功能。因此,每次创建新包时,我都会为其分配一个barcode_number。这是我要创建的全局变量。每次我将它分配给一个新包时,我会将它增加一个,从而确保两个包永远不会有相同的数字。
答案 0 :(得分:1)
我想到了这种模式:
if (!$request = $this->requestStack->getMasterRequest()) {
return $path;
}
调用absolute_url
(或class Permanent
def self.increment
@@val ||= 0
@@val += 1
end
def self.value
@@val ||= 0
end
end
只读取当前值)。显然,您可以将类和方法重命名为更方便。您可以使起始值不等于零(或从数据库中读取或以其他方式计算它)。重点是,为了您的目的,该类将表现为单身。
答案 1 :(得分:0)
The solution I found for this problem was that every time I need to assign a barcode number to a new package, I just look at the most recently added package, retrieve its barcode number, add one to that and assign this new number to the new package. It looks something like this:
barcode = ButtonFinishDiv.last.barcode + 1
ButtonFinishDiv.create(..., barcode: barcode)