Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#...
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
You can’t perform that action at this time.