Search code examples
rubysequel

How to update updated_at field for a Sequel dataset?


I am using Sequel gem to update a dataset.

If you do an update on the model instance, the updated_at timestamp gets updated just fine:

foo = Foo[id: 1]
foo.update(blah: blah)

However, when you call a batch update on the dataset, it does not update the updated_at timestamps for every entry.

Foo.where(id: 1).update(blah: blah)

I looked at the Sequel docs regarding the timestamps plugin, which I am using in my code already, but the updated_at fields will still not be updated using the batch update. Can someone help me with this issue?


Solution

  • Running Foo.where(id: 1).update(blah: blah) executes a single query on the database, which is not going to run model instance-level hooks. You can either update each record individually:

    Foo.where(id: 1).all{|f| f.update(blah: blah)}

    or you can switch to a database-trigger based approach, such as https://github.com/jeremyevans/sequel_postgresql_triggers