Rails生成数字

时间:2014-08-19 06:17:15

标签: ruby-on-rails

我的rails应用程序报告编号从1001,1002开始。

当iam创建新报告时,报告编号统计数据来自1001..1002

t.integer  "reportnumber"

1 个答案:

答案 0 :(得分:2)

class CreateSimpleModels < ActiveRecord::Migration
  def self.up
    create_table :simple_models do |t|
      t.string :name
      t.integer :reportnumber
      t.timestamps
    end
    execute "CREATE SEQUENCE simple_models_user_number_seq OWNED BY
simple_models.reportnumber INCREMENT BY 1001 START WITH 1"
  end

  def self.down
    drop_table :simple_models
    execute "DELETE SEQUENCE simple_models_user_number_seq"
  end
end

在创建回调之前使用:

before_create :update_report_number

def update_report_number
  self.report_number = Report.last.report_number + 1
end