- class ApplicationController < ActionController::Base
- 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 < ActionController::Base
- 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.
阅读(422) | 评论(0) | 转发(0) |