Search code examples
activerecordruby-on-rails-3.1

Changing updated_at in a Rails record


I have a memory based session table containing the following:

id

sessnhash

userid

created_at

updated_at

The idea is that when a user first logs in a random sessnhash is created and passed back so that all further actions have to send that sessionhash for server requests. These go through a loggedin? method on the application controller which simply checks that a row in the table exists for that sessionhash.

I now want to extend this functionality to keep a tab of the last time any activity happened for that user and I thought one of way of doing this would be to immediately do a save after finding the sessionhash, hoping that this would then update the 'updated_at' attribute.

However Rails is too clever and doesn't update anything as in reality nothing has been updated.

Is there any way of forcing Rails to update the updated_at attribute without having to make any data changes to any of the other attributes?


Solution

  • I've just learnt that I need to do.

    sessn.touch 
    

    which will update the updated_at attribute.

    It's all in here: touch.