What is user_id


#1

In the create controller there is a portion of the code where it says

if @user.save
session[:user_id] = @user.id

what is :user_id. I know it is a key in the sessions array but where will it come from? How will it be generated

https://www.codecademy.com/courses/rails-auth/lessons/authentication/exercises/authentication-signup-ii?action=resume_content_item


#2

Without looking at your code, I hope this snippet helps

 h = {}
=> {}
 h[:id] = 2
=> 2
 h
=> {:id=>2}

You can create keys/values this way


#3

so if there are 10 users created by the database rails will just automatically give :user_id values from 0 to 9


#4

@user = User.new(user_params) will generate a object and run the new method of User class. This method will create the user id (or the new method calls another method which generated id), which is very likely achieved by using the databases incremental unique primary key id field

but you would need to find the source code of User class to confirm this


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.