class Users::MySessionsController < Devise::SessionsController after_action :after_login, :only => :create # Hook after successful login def after_login Log.log "User #{current_user} logged in." old_screenshots = session[:uploaded_screenshots] if old_screenshots.to_a.length > 0 old_screenshots.each do |id| next unless id # sometimes we had 'nil' here ss = Screenshot.find(id) ss.user = @user ss.save! end # The message is probably confusing. Just add the screenshots. # flash[:info] = "#{old_screenshots.to_a.length} uploads have been added to your account" Log.log "Anonymously uploaded screenshots #{old_screenshots} added to #{current_user}." session[:uploaded_screenshots] = nil end end end