- 论坛徽章:
- 0
|
class ApplicationController def current_user @current_user ||= User.find(session[:user_id]) end end
this piece of codes are much better than the following pieces:
class ApplicationController def current_user User.find(session[:user_id]) end end
Because in the first part, the searching result is saved in the instance variable, next time when get the current user, it will not execute the search again, it just return that variable.
But in the second piece of code, it will excute the search everytime you get the current user.
this can improve the performance.
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/25407/showart_1902378.html |
|