Skip to content

Instantly share code, notes, and snippets.

@bwillis
Created March 22, 2012 16:29
Show Gist options
  • Save bwillis/2159383 to your computer and use it in GitHub Desktop.
Save bwillis/2159383 to your computer and use it in GitHub Desktop.
Fix unprecise MySQL dates for active record
class ActiveRecord::Base
DATE_FIELDS = [:created_at, :updated_at]
after_save :make_dates_less_precise
def make_dates_less_precise
DATE_FIELDS.each do |date_field|
if respond_to? date_field
time = self.send(date_field)
write_attribute(date_field, Time.at(time.to_i)) unless time.nil?
end
end
end
end
require 'spec_helper'
describe "MonkeyPatch MySQL imprecise dates" do
context "given a newly saved model" do
before do
@object = Factory :user
end
it "has dates that are equal to the looked up object" do
User.find(@object.to_param).created_at == @object.created_at
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment