Reputation: 2853
In model/item.rb
I have custom validation method
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
Validation method does work in "rails c". But, when I run two task at the same time (rails runner "ApplicationController.rotate"
I have Item.save in rotate method) - validation stops working.
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
Any hints or advice would be appreciated.
Upvotes: 0
Views: 79
Reputation: 2853
Thanks to @FabKremer I was able to solve it :)
by adding @i.created_at = Time.now.change(usec: 0)
before Item.save
:)
Nevertheless I'm accepting his answer, cuz it'll work for most cases.
Upvotes: 0