I`m currently reading Programming Phoenix and struggle to understand some parts of the code.
[web/models/user.ex] def changeset(model, params \\ :empty) do model |> cast(params, ~w(name username), ) # WTF is ~w? Never explained |> validate_length(:username, @username_validation_length) end
~w/1 function and this strange name? What does
When I look up the docs for
cast/3 , I can just replace
[:name, :username] but I wan’t to know what’s the difference (besides eye cancer).
Another problem is the following code:
[web/controllers/auth.ex] def call(conn, repo) do user_id = get_session(conn, :user_id) user = user_id && repo.get(Rumbl.User, user_id) # && should return boolean assign(conn, :current_user, user) end
The docs are not really helpful in describing
Returns session value for the given key
I can assume that
get_session/2 returns an int (but not because of the docs…).
I dont know what’s going on on the other lines of code, he is using this session id to get a user from the repo, but I thought the session id would be some kind of random value…
After that, I dont know whats going on with user (an int) and the returned struct…He is comparing them somehow…how is this working?
Thanks for taking time and reading this, I hope you can help me.