The following blog post contains material either currently found or soon to be incorporated into my new book, "Easy Active Record for Rails Developers". Among many other topics, you'll learn about model generation, migrations, validations, associations, scopes, joins, includes, forms integration, nested forms, and model testing with RSpec and FactoryGirl. The book is now available, head over to this website's home page to learn more.


Occasionally I’ll need to run a Rake task repeatedly, passing in a specific date with each iteration. If the task involves iterating more times than I care to repeatedly adjust a date from the command line, I’ll generate an array consisting of the set of desired dates and then loop over that array, executing the Rake task with each iteration. For instance, suppose the task requires the date to be formatted like YYYY-MM-DD (2013-12-09 for instance), and we require an array of dates ranging between 2013-12-01 and today, which happens to be 2013-12-12. You can generate the array using the following code:

("2013-12-01".to_date..Date.today).map{|date| date.strftime("%Y-%m-%d")}

Executing this code generates the following array:

["2013-12-01", "2013-12-02", "2013-12-03", "2013-12-04", "2013-12-05", 
 "2013-12-06", "2013-12-07", "2013-12-08", "2013-12-09", "2013-12-10", 
 "2013-12-11", "2013-12-12"]

Like what you read? There’s plenty more where this came from in my new book, “Easy Active Record for Rails Developers”!

Comments