Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我想在rails中从001开始编号_Ruby On Rails - Fatal编程技术网

Ruby on rails 我想在rails中从001开始编号

Ruby on rails 我想在rails中从001开始编号,ruby-on-rails,Ruby On Rails,在预订课上,我指定了从哪个开始。代码是这样的 class Booking < ActiveRecord::Base MAGIC_NUMBER = 001 # order ref start from 001 to add some obscurity def generate_ref_sequence count = Booking.new_today.count self.ref = "#{market}#{Date.today.strftime("%y%m%d")}

在预订课上,我指定了从哪个开始。代码是这样的

class Booking < ActiveRecord::Base
MAGIC_NUMBER = 001 # order ref start from 001 to add some obscurity
def generate_ref_sequence
    count =  Booking.new_today.count
    self.ref = "#{market}#{Date.today.strftime("%y%m%d")}#{"%d" % (MAGIC_NUMBER+count+1)}"

  end
生成序列时没有错误。但我想要131220009港币,但我得到1312209港币。我想生成001,002,003,然后010,011,012,然后099,然后100,然后101。。。。依此类推。

将%d更改为%03d将解决您的问题

或者,您可以使用%s和MAGIC_NUMBER+count+1.to_us.rjust3,“0”

%03d%1使用此选项可根据需要格式化数字