I'm currently in the process of removing IP logging from our app, I was wondering what's the best way to go about doing this with Devise?
Asked
Active
Viewed 666 times
2 Answers
3
Your answer looks good, but if you're looking to track the IP for only specific users, one (less verbose but perhaps more confusing) alternative, could be...
protected
# Override Devise logic for IP tracking
# https://github.com/plataformatec/devise/blob/master/lib/devise/models/trackable.rb#L45
def extract_ip_from(request)
# Only track the IP for admin users (per GDPR rules).
request.remote_ip if admin?
end
This would cause a nil
IP to be set for non-admin users.

Ollie Bennett
- 4,424
- 1
- 19
- 26
-
True, but with the way I set it out, you can be selective about any of the trackables ,not just IP, but for just IP I would say this works best. – Thermatix May 14 '18 at 10:35
-
1True! :) Cheers for the question anyway - really useful! – Ollie Bennett May 14 '18 at 11:37
2
Adding this method to the user model can allow you to be selective in what you track, in my case I'm being selective in when I track IP addresses:
def update_tracked_fields(request)
old_current = current_sign_in_at
new_current = Time.now.utc
self.last_sign_in_at = old_current || new_current
self.current_sign_in_at = new_current
if admin?
old_current = current_sign_in_ip
new_current = request.remote_ip
self.last_sign_in_ip = old_current || new_current
self.current_sign_in_ip = new_current
end
self.sign_in_count ||= 0
self.sign_in_count += 1
end

Thermatix
- 2,757
- 21
- 51