views:

1156

answers:

1

I have a process which iterates through a bunch of ActiveRecord models, does some processing, and saves the models again. Often though, the processing doesn't result in an changes to the attributes and so the updated_at column never changes (even though save is called).

I'd prefer not to disable partial updates (in general, they're useful). I'm guessing that my two options are to:

  1. Add a separate timestamp column to the model (i.e., 'processed_at') and manage this myself, although this seems a bit wasteful/redundant.
  2. Or somehow override the management of the updated_at attribute?

I've heard that Rails 3 will have a 'touch' method which would be exactly what I'm looking for.

Any ideas/options/opinions?

+9  A: 

Actually the touch method is already in Rails 2.3.x, so you can simply do:

model.touch

To update the updated_at column. Alternatively, to update some other column with the current date and time use:

model.touch(:column_name)
John Topley
Wow! You're right. Lesson learnt, don't believe everything you read in a blog post (http://ryandaigle.com/articles/2009/4/20/what-s-new-in-edge-rails-touching), just go direct to the source (http://github.com/rails/rails/blob/0d3c5f0a822cd1b6029b5f619774b7794a94f370/activerecord/lib/active_record/timestamp.rb#L27)
hopeless
I think DHH may have mentioned it on Twitter - that's how I knew about it.
John Topley
Grrr... it looks as if the touch support just missed the 2.3.2 release and is only in the 2.3-STABLE branch. There's no 2.3.3 milestone but 2.3.4 is listed as July 18th. I guess I'll just have to wait :-(
hopeless