1

I have read that it is possible to store sessions in rails without user authentication (referenced this), however I am not sure how to go about it in my user model. I am basically creating a survey and only want to store the user's IP address and ID (I cannot have any other identifiable information for IRB purposes). The user will be referenced by another model, and I want responses of the survey to be owned by the current user. Tips?

Community
  • 1
  • 1
knod
  • 31
  • 5
  • Where does the ID come from? – Ruslan Mar 02 '17 at 20:05
  • @Ruslan I am not sure. That is part of my problem. I basically want to create a user without traditional credentials, so I am not sure how the ID would be generated. – knod Mar 02 '17 at 20:13
  • It's generally better to post actual code snippets so we have more concrete examples of your problem to help solve. Asking a fairly general question like this makes it hard to answer, and thus unhelpful to the site. Please post some code snippets and specific questions you would want help with, thanks! – thaavik Mar 02 '17 at 21:40

2 Answers2

0

You could use securerandom to generate the unique ID's for users

require 'securerandom'
SecureRandom.uuid

Then you can store that in the session to keep track of the user

def survey_user
  session[:survey_user] ||= SecureRandom.uuid
  SurveyUser.find_or_create_by(id: SecureRandom.uuid)           
end

So now when a user comes to do your survey, you have a model for them in database, and can create associations with their answers

Ruslan
  • 1,919
  • 21
  • 42
0

You don't really need to create a session. It will be created automatically when needed, i.e. when you store something in it. You should just need something like:

session[:id]         = ...
session[:ip_address] = ...

(Although there should be no need to store the ip address at all since it should always be available in the request object)

Marc Rohloff
  • 1,332
  • 7
  • 8