Note that there are some explanatory texts on larger screens.

plurals
  1. POauthlogic current_user tracking
    text
    copied!<p>I use authlogic for authentication and paperclip for handling user's profile picture attachments. </p> <p>I use the following method to get the current_user</p> <pre><code> def current_user_session return @current_user_session if defined? (@current_user_session) @current_user_session = UserSession.find end def current_user return @current_user if defined?(@current_user) @current_user = current_user_session &amp;&amp; current_user_session.record end </code></pre> <p>and the following after_save callback to regenerate the profile image if the image has changed.</p> <pre><code> after_save do |user| if user.image_changed? Delayed::Job.enqueue ImageJob.new(user.id) end end </code></pre> <p>The current_user method issues a UserSession.find call and Looking at the docs it seems UserSession.tries to Log the user in, which results in an update to certain fields (updated_at, token etc. but not the profile image), which results in a save of the user record, which in my case results in after_save callbacks firing. This after save callback checks to see if there has been a change to user's profile image, which unnecessarily consumes time.</p> <p>Now this is fine if the user indeed is trying to update profile, but since I use current_user in many different places within my app, this callback is getting fired (and its an expensive call), for no reason.</p> <p>I understand this isn't exactly authlogic issue, but is there anyway I can avoid this, i.e. either not update the user record or somehow differentiate between what is a profile update and what is an update resulting from this UserSession.find login?</p> <p>Thanks </p> <p>Here is the solution that worked based on bjg's suggestion.</p> <p>before_save :check_what_changed</p> <p>def check_what_changed if self.changed.sort == ["last_request_at", "perishable_token"] self.skip_profile_update = true return true else self.skip_profile_update = false return true end end</p> <p><strong>Update #2 I take that back that didn't work, but it's not really a problem with this solution, for some reason paperclip tries to save the attachments even before the after_save callback. This is likely a paperclip issue, no idea..</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload