I have a standard query that gets the current user object:
@user = User.find_by_email(session[:email])
but I’m putting it as the first line in every single controller action which is obviously not the best way to do this. What is the best way to refactor this?
Do I put this as a method in the Application controller (and if so, can you just show me a quick example)?
Do I put the entire @user object into the session (has about 50 columns and some sensitive ones like is_admin)?
Or is there another way to remove this kind of redundancy?
I suggest making it into a helper placed in the
ApplicationHelpermoduleI prefer the above usage instead of the standard
@user ||= User.find...because it prevents repetitive queries if the user record isn’t found the first time. You could also just bang the find method:find_by_email!to make it throw an exception when the user can’t be found