# How to DRY with Fixtures (helper or extend Time class, how)?

**URL:** <https://www.ruby-forum.com/t/how-to-dry-with-fixtures-helper-or-extend-time-class-how/60017>\
**Category:** Rails\
**Created:** [January 7, 2006, 11:21am UTC](https://www.ruby-forum.com/t/how-to-dry-with-fixtures-helper-or-extend-time-class-how/60017 "2006-01-07T11:21:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@henrik](https://www.ruby-forum.com/u/henrik)\
**Post date:** [January 7, 2006, 11:21am UTC](https://www.ruby-forum.com/t/how-to-dry-with-fixtures-helper-or-extend-time-class-how/60017/1 "2006-01-07T11:21:04Z")

</div>

I have a test/fixtures/users.yml like so:

apa:  
id: 1  
username: apa  
[…]  
created\_at: \<%=Time.now.strftime("%Y-%m-%d %H:%M:%S")%\>  
updated\_at: \<%=Time.now.strftime("%Y-%m-%d %H:%M:%S")%\>

I don’t like how I’m repeating myself with the strftime bit.

Is there some shorter Time method to format time for a (MySQL) datetime  
field that I’ve missed? The magic of created\_at/updated\_at does not seem  
to work with fixtures.

Assuming I want to avoid repeating myself, in what file would I add a  
helper method for this, or extend the Time class with a method?  
Apparently not test\_helper - “undefined local variable or method”.

---

<div class="post-metadata">

**Author:** ![henrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ba9def/32.png) [@henrik](https://www.ruby-forum.com/u/henrik)\
**Post date:** [January 7, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/how-to-dry-with-fixtures-helper-or-extend-time-class-how/60017/2 "2006-01-07T20:34:02Z")

</div>

On Sat, Jan 07, 2006 at 11:21:04AM +0100, Henrik wrote:

> Is there some shorter Time method to format time for a (MySQL) datetime  
> field that I’ve missed? The magic of created\_at/updated\_at does not seem  
> to work with fixtures.

created\_at: \<%= 5.days.ago.to\_s(:db) %\>  
updated\_at: \<%= 18.minutes.ago.to\_s(:db) %\>

You can add custom Time and Date strftime formats by adding key/value  
pairings (in e.g. your config/environment.rb) to the  
ActiveSupport::CoreExtensions::Time::Conversions::DATE\_FORMATS and  
ActiveSupport::CoreExtensions:📅:Conversions::DATE\_FORMATS hashes.

For example:  
ActiveSupport::CoreExtensions::Time::Conversions::DATE\_FORMATS.merge!  
:day =\> ‘%Y-%m-%d’

marcel
