Match build_stubbed's created_at type to ActiveRecord #469

Closed
wants to merge 2 commits into
from

2 participants

@aripollak

From thoughtbot/factory_girl_rails#85 :

TZ=UTC rails c
>> Time.zone #=> (GMT-05:00) Eastern Time (US & Canada)
>> Timecop.freeze 2012, 9, 13
>> FactoryGirl.build_stubbed(:ach_transfer).created_at #=> 2012-09-13 00:00:00 +0000
>> FactoryGirl.create(:ach_transfer).created_at #=> Wed, 12 Sep 2012 20:00:00 EDT -04:00

FactoryGirl's stubbed created_at seems to use Time.now, whereas AR's uses Time.zone.now. factory_girl_rails should have the correct AR behavior for stubbed AR records.

@joshuaclayton
thoughtbot, inc. member

Thanks for the PR @aripollak! I quickly looked up ActiveRecord's calculation for assigning created_at, and it looks to differ a bit from this: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/timestamp.rb#L101-L103 Can you explain why their solution wouldn't work? Any insight would be really helpful! Thanks, looking forward to getting this resolved!

@aripollak

I think what you're asking is why AR can use Time.now but it's incorrect for FactoryGirl. If so, it's because that code is specific to writing created_at/updated_at. It still gets converted to a TimeWithZone, I believe in https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb#L36-L59.

@joshuaclayton
thoughtbot, inc. member

Ah, there it is! Once Travis reports this as green, I'll get it in; thanks again @aripollak!

@aripollak

Thanks! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment