Iterating over all Models in Rails

Posted by Matt on May 16, 2008

Neat little snippet of code in those odd times you need to loop over every model in your application programmatically.

This exact snippet loops over every model, and converts each timestamp (created_at, updated_at) to UTC. It can be quite helpful if you’re adding Time Zone support to your Rails 2.1 application and you have existing non-UTC timestamps.


Object.constants.each do |o|
  klass = eval(o, TOPLEVEL_BINDING)
  if klass.is_a?(Class) && klass.superclass == ActiveRecord::Base
    puts "\n\nUpdating #{klass} timestamps to UTC"
    klass.all.each do |obj|
      offset_hours = (Time.zone.utc_offset / 1.hour) * -1
      obj.created_at = obj.created_at + (obj.created_at.dst? ? offset_hours - 1 : offset_hours).hours
      obj.updated_at = obj.updated_at + (obj.updated_at.dst? ? offset_hours - 1 : offset_hours).hours
      obj.save
      print '.'
    end
  end
end

*Please note: I haven’t tested this very thoroughly, but it does seem to do the trick. As we know, Dates and Times can be total PITAs!

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

Comments