我想要以这种格式组织发票:
AB20140001
AB
是我们公司的首字母缩写,20140001
是2014年的第一张发票。目前的快速思路是在表中添加invoices
列 year
和 invoice_number
以及存储位置 2014
和当前发票编号(0001
表示第一个一个)。
但是,如何以 XXXX
格式保留 invoice_number
列(第二张发票的编号为 0002
等等) ?
或者 - 是否有更好的方法来组织发票编号?
提前谢谢你。
最佳答案
我会做一个在创建和设置序列号之后运行的函数
我们使用 :after_create
以便我们可以在设置后使用 :id
。
class Invoice < ActiveRecord::Base
after_create :set_invoice_numeber
def set_invoice_number
self.update_attribute(:invoice_number, 'AB' + Time.now.year + '%.4d' % id)
end
end
关于mysql - Rails + MySQL 中的发票编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25606603/