You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In ActiveRecord, you can't set the created_at or updated_at timestamps through the use of #save. At best, you can update the created_at timestamp by using #update_attribute in a second command after the object has been created, but you can't get away with even that for updated_at -- you have to execute SQL.
Since setting created_at and updated_at timestamps are often useful in test environments, I'm asking that FactoryGirl have a special case for created_at and updated_at that lets them be overridden.
The following should work as expected:
user = Factory :user, :created_at => 2.days.ago, :updated_at => 1.day.ago
user.created_at #=> 2 days ago
user.updated_at #=> 1 day ago
user = Factory.build :user, :created_at => 2.days.ago, :updated_at => 1.day.ago
user.save
user.created_at #=> 2 days ago
user.updated_at #=> 1 day ago
The latter seems much more difficult to implement than the former -- the only way I could think to do it would be to instance_eval an after_create callback on the built object, to make sure that it'd get updated appropriately down the line. However, I'd be happy even if only the first form works.
Similarly, the following should work as expected:
Factory.define :user do |u|
u.created_at {Factory.next :user_created_at}
end
Factory.sequence(:user_created_at) {|i| i.days.ago}
I'm interested in hearing people's opinions on this.
The text was updated successfully, but these errors were encountered:
Instead of what you are proposing above, I think it's better to fake Time.now to whichever time you want in your tests, and then create objects like you normally do. That way, ActiveRecord still does the magic timestamping for you, and you have control of whether you fake the timestamps or not per-test.
Reported by Eric Mill
In ActiveRecord, you can't set the created_at or updated_at timestamps through the use of #save. At best, you can update the created_at timestamp by using #update_attribute in a second command after the object has been created, but you can't get away with even that for updated_at -- you have to execute SQL.
Since setting created_at and updated_at timestamps are often useful in test environments, I'm asking that FactoryGirl have a special case for created_at and updated_at that lets them be overridden.
The following should work as expected:
The latter seems much more difficult to implement than the former -- the only way I could think to do it would be to instance_eval an after_create callback on the built object, to make sure that it'd get updated appropriately down the line. However, I'd be happy even if only the first form works.
Similarly, the following should work as expected:
I'm interested in hearing people's opinions on this.
The text was updated successfully, but these errors were encountered: