Skip to content

Instantly share code, notes, and snippets.

@bookis
Last active January 1, 2016 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bookis/8202150 to your computer and use it in GitHub Desktop.
Save bookis/8202150 to your computer and use it in GitHub Desktop.
#...
def current_visitor # This could be any name, `current_visitor` is just descriptive
if session[:visitor_id] # checking if the session hash has a key of :visitor_id
Visitor.find(session[:visitor_id]) # If there is a visitor id, find the Visitor by what is in the value of session[:visitor_id]
else
visitor = Visitor.create(ip_address: request.remote_ip) # If no session visitor id, create a new visitor assigning the ip_address
session[:visitor_id] = visitor.id # set the session hash visitor_id key to be the id of the record just created
visitor # return the visitor object that you just created, otherwiet the line above would be the last thing returned, but we want a Visitor object to be returned
end
end
#...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment