简体   繁体   English

Rails中DateTime.now.strftime('Y%m%d%H%M%S')正确的字符串格式

[英]DateTime.now.strftime('Y%m%d%H%M%S') proper string format in Rails

I'm trying to build a reference number for booking in Rails. 我正在尝试建立一个用于在Rails中进行预订的参考号。 I'm using 我正在使用

DateTime.now.strftime('Y%m%d%H%M%S')

for the reference number and concatenate some string. 为参考数字并连接一些字符串。 I used Sidekiq for performing this as a background job but then was confused with the result because it gave a different value in strftime . 我使用Sidekiq作为后台作业来执行此操作,但后来却对结果感到困惑,因为它在strftime给出了不同的值。

def to_reserve
   venue = Venue.find(params[:id])
   venue_name = venue.name.delete(' ')
   reference_number = ReferencesNumberWorker.perform_in(5.seconds,    venue_name) 

   ................

end

Sidekiq: Sidekiq:

class ReferencesNumberWorker
    include Sidekiq::Worker

    def perform(venue_name)
        reference_number = DateTime.now.strftime('Y%m%d%H%M%S')
        return venue_name + reference_number
    end
end

This is a sample result: 这是一个示例结果:

HotelMatt67fb02cb5da7871f92347df9

which I didn't expect with the right result. 我没想到会得到正确的结果。

That's appending the hash for a Sidekiq job to your reference number where you are expecting it to append the return of the Sidekiq result. 那就是将Sidekiq作业的哈希值附加到您的参考号上,您希望该哈希值附加Sidekiq结果的返回值。

So essentially this method 所以本质上这个方法

def to_reserve
   venue = Venue.find(params[:id])
   venue_name = venue.name.delete(' ')
   reference_number = ReferencesNumberWorker.perform_in(5.seconds,    venue_name)  <- This line will return Sidekiq job number, not the actual result from Sidekiq perform operation. 

   ................

end

When you define reference number you're assuming it'll give you the Sidekiq return, which it won't. 当您定义参考号时,您会假设它会给您Sidekiq返回值,而不会。 This is the fundamental point of background jobs. 这是后台作业的基本要点。 You're disconnecting the Sidekiq job from what you're doing, ie offloading to a separate process. 您正在断开Sidekiq作业与正在执行的操作的连接,即,将其卸载到一个单独的进程中。 So you can't expect a return, you're offloading the method to Sidekiq. 因此,您不能期望得到回报,而是将方法卸载到Sidekiq。 Sidekiq's return is basically the job number. Sidekiq的回报基本上是工作编号。

If you want this to work then move the reference number computation that you're doing to Sidekiq in the to_reserve method OR if it must be done in Sidekiq move all of the logic from to_reserve to Sidekiq's perform method. 如果您希望此方法有效,则将您正在执行的参考数字计算通过to_reserve方法移至Sidekiq,或者如果必须在Sidekiq中进行,则将所有逻辑从to_reserve移至Sidekiq的perform方法。

Also note, to use Sidekiq you should pass (to Sidekiq) the object's id so you can relook it up in Sidekiq and perform some idempotent operation on the object. 还要注意,要使用Sidekiq,您应该将对象的ID传递(传递给Sidekiq),以便可以在Sidekiq中查找它并对该对象执行一些幂等操作。 In my opinion, I don't think in this case you need to use Sidekiq. 我认为,在这种情况下,您不需要使用Sidekiq。 Its unnecessary complexity, but to each their own. 它不必要的复杂性,但是要各有千秋。

Sidenote: Prefer the Time class ( Time#now vs. DateTime#now ) over DateTime to compute the strftime method. 旁注:与DateTime#now相比,首选Time类( Time#nowDateTime#now )来计算strftime方法。 Its a pure Ruby method and is faster than the DateTime class (a known performance bottleneck). 它是纯Ruby方法,并且比DateTime类(已知的性能瓶颈)要快。 Also your code won't have a hard dependency on Rails. 同样,您的代码对Rails也不会有严格的依赖性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将默认日期格式更改为%Y%m%d%H%M%S - Change default date format to %Y%m%d%H%M%S 如何在轨道3.2.12中发送(“strftime(&#39;%Y /%m /%d&#39;)”)? - How to send(“strftime('%Y/%m/%d')”) in rails 3.2.12? NoMethodError(“%m /%d /%Y%H:%M”的未定义方法“ year”:字符串) - NoMethodError (undefined method `year' for “%m/%d/%Y %H:%M”:String) 有没有办法将 Rails 默认时间戳更改为 Ymd H:i:s(而不是 Ymd H:i:su)或让 Laravel 忽略 Ymd H:i:su 的小数部分? - Is there a way to change Rails default timestamps to Y-m-d H:i:s (instead of Y-m-d H:i:s.u) or have laravel ignore decimal portion of Y-m-d H:i:s.u? 将带有Rails的数据库中的datetime更改为简单的%m /%d /%Y - Change datetime to simple %m/%d/%Y in database with rails 如何将Rails%d /%m /%Y日期格式字符串转换为dd / mm / yyyy? - How to convert Rails %d/%m/%Y date format string to dd/mm/yyyy? 创建日期属性以“%m /%d /%Y”格式传递的Rails对象 - Creating rails object with date attribute passed in “%m/%d/%Y” format rails db不会将字符串保存为m / d / y格式的日期? - rails db won't save string as date with m/d/y format? 通过以下格式在日期中设置和显示日期:%m /%d /%Y - Set and Display Dates in rails via format: %m/%d/%Y MySQL日期时间格式%m-%d-%Y以rails形式抛出超出范围的错误 - mysql datetime formatted %m-%d-%Y throws an out of range error in rails form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM