mysql - Rails + MySQL 中的发票编号

标签 mysql sql ruby-on-rails ruby invoices

我想要以这种格式组织发票:

AB20140001

AB是我们公司的首字母缩写,20140001是2014年的第一张发票。目前的快速思路是在表中添加invoicesyearinvoice_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/

相关文章:

mysql - 如何获得期间的天数?

mysql - SQL 将值拆分为多行

MySQL更改文本列中的文件扩展名

java - 在 eventStartTime 三天前发送电子邮件

javascript - 我需要带有浮点图插件值的 JSON 格式

php - UNION 查询在 mysql 中工作,但在 php 中工作不一样

ruby-on-rails - Rails chartkick : want only integer values on axes. 使用离散的还是其他的?

ruby-on-rails - 嵌套形式/茧 : children not saving

ruby-on-rails - 由于缺少 sqlite3 gem,Rails 4.0rc1 应用程序未运行

php - 在面向对象的 PHP 中连接到数据库