Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent the updated_at field in User to get modified while signing in using devise

I have a situation where I need to use the updated_at field of all the models to create a synchronize functionality. But In case of User model, the updated_at field gets modified every time a user signs in. Even worse is the case, when a device signs in with a user's credentials to sync data updated_at field just gets modified before sync has even started.

So, My question is, Is there a way we can stop devise to update the updated_at field when a user signs in?

like image 334
Manoj Monga Avatar asked Dec 26 '22 09:12

Manoj Monga


1 Answers

Here's what I did: override the should_record_timestamps? method on the devise model to not change the updated_at column when only the Devise fields are updated. Here's the code, it handles Devise::Trackable and Rememberable.

  def should_record_timestamps?
    (self.changes.keys.map(&:to_sym) - (Devise::Models::Trackable.required_fields(nil) + [:remember_token, :remember_created_at])).present? && super
  end

This is a great way of not invalidating the caches when a user logs in.

like image 97
iwiznia Avatar answered Dec 28 '22 23:12

iwiznia