What is user_id


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



Without looking at your code, I hope this snippet helps

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

You can create keys/values this way


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


@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


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