在model/item.rb
我有自定义验证方法
validate :double_dates
after_save :double_check
private
def double_dates
if Item.where(:user_id => self.user_id, :asin => self.asin, :domain => self.domain).where("DATE(created_at) = ?", Date.today).length >= 1
errors.add(:created_at, "no double dates")
end
验证方法在“rails c”中有效。但是,当我同时运行两个任务时(rails runner "ApplicationController.rotate"
我在旋转方法中有Item.save) - 验证停止工作。
irb(main):044:0> i1.created_at
=> Thu, 24 Apr 2014 02:41:15 UTC +00:00
irb(main):045:0> i2.created_at
=> Thu, 24 Apr 2014 02:41:15 UTC +00:00
irb(main):046:0> i1.created_at == i2.created_at
=> false
irb(main):047:0> i1.created_at.to_time.to_i == i2.created_at.to_time.to_i
=> true
irb(main):048:0> i1.created_at.class
=> ActiveSupport::TimeWithZone
irb(main):049:0> i2.created_at.class
=> ActiveSupport::TimeWithZone
irb(main):050:0> i1.created_at.usec => 311714
irb(main):051:0> i2.created_at.usec => 312779
任何提示或建议都将不胜感激。
答案 0 :(得分:2)
答案 1 :(得分:0)
感谢@FabKremer,我能够解决它:)
在@i.created_at = Time.now.change(usec: 0)
之前添加Item.save
:)
尽管如此,我还是接受了他的回答,因为大多数情况下都会这样做。