Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Cache Key with updated_at #81

Open
dewski opened this Issue · 5 comments

6 participants

@dewski

I realized using cache_key on any Authlogic based model will never really cache, because the updated_at attribute is always changing per refresh.

Here is the SQL that seems to be updating the user on refresh
UPDATE users SET updated_at = '2009-12-14 15:30:50', perishable_token = '6S-x8Qoy-18BaLadImBG', last_request_at = '2009-12-14 15:30:50' WHERE id = 7

Is there any way to remove the updated_at attribute from this?

@ahawkins

ya...this is by far the most annoying thing about authlogic

@pekeler

+1

@guzart

After reading this post http://blog.bigbinary.com/2009/01/21/override-automatic-timestamp-in-activerecord-rails.html I added the following code to my Authlogic authentic model.

Please note that this code works for me in Rails 2, haven't tested it in later versions.

# Save the original class level inherited record timestamps value
alias_method :class_record_timestamps, :record_timestamps
# Gets the instance level record timestamps or fallback to the original value
def record_timestamps; defined?(@instance_record_timestamps) ? @instance_record_timestamps : class_record_timestamps; end
# Set the instance level record timestamps value
def record_timestamps=(value); @instance_record_timestamps = value; end

# Save the original save without session maintenance method
alias_method :orig_save_without_session_maintenance, :save_without_session_maintenance

# Override the save_without_session_maintenance method and wrap the save with
# disabling the record_timestamps on update.
def save_without_session_maintenance(*args)
  self.record_timestamps = false
  orig_save_without_session_maintenance(*args)
  self.record_timestamps = nil
end
@tiegz
Collaborator

@guzart nice. I think this wouldn't work in Rails 4 because of a regression, but there's another way you might do it:

  private
  def should_record_timestamps?
    changed_keys = self.changes.keys - ["last_request_at", "perishable_token", "updated_at", "created_at"]
    changed_keys.present? && super
  end

I also overwrite my User#reset_perishable_token method such that it refreshes less often.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.