Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Key with updated_at #81

Closed
dewski opened this issue Dec 14, 2009 · 6 comments

Comments

@dewski
Copy link

commented Dec 14, 2009

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

This comment has been minimized.

Copy link

commented Oct 23, 2010

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

@pekeler

This comment has been minimized.

Copy link

commented Mar 27, 2013

+1

1 similar comment
@johnnaegle

This comment has been minimized.

Copy link
Contributor

commented Mar 12, 2014

+1

@guzart

This comment has been minimized.

Copy link

commented Jun 17, 2014

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

This comment has been minimized.

Copy link
Collaborator

commented Jun 17, 2014

@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.

@jaredbeck

This comment has been minimized.

Copy link
Collaborator

commented Feb 19, 2017

Given that Tieg has provided a workaround, and there has been no activity on this issue in the three years since, I will close it.

@jaredbeck jaredbeck closed this Feb 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.