getting session user_id in a model
I have 开发者_如何学Go"updated_by" field in most of my models. I need to update this field with the current user id.
What is the best way to obtain session user is in a Model?
Where can I put logic to populate the "updated_by" field for any models which respond_to that method?Thanks
I suggest you to store the current_user in the ApplicationController. You can handle there any logic you want (read the cookie, redirect to the home page if the user have not cookie etc). So storing the current_user variable in the ApplicationController let you access to that from every model or controller you want.
Check this link http://niczsoft.com/2010/03/rails-current-user/
精彩评论