class ApplicationController < ActionController::Base
def current_user
User.find(session[:user_id])
end
end
上面代码每次请求都会查询一次数据库。下面用实例变量解决这个问题。
@current_user ||= User.find(session[:user_id])
很简单,不多说了...
class ApplicationController < ActionController::Base
def current_user
@current_user ||= User.find(session[:user_id])
end
end
分享到:
相关推荐
• Table of Contents • Index ... Automatically Initializing Instance Variables from _ _init_ _ Arguments Recipe 6.19. Calling a Superclass _ _init_ _ Method If It Exists Recipe 6.20. ...
In previous versions of Oracle, result caches were private to each instance in a Real Application Clusters (RAC) environment. However, in 11g Release 2, result caches are now shared across all ...
* When running a source file in explorer, don't spawn new instance. Instead open the file in an already launched Dev-C++. * Class-parser speed-up (50% to 85% improvement timed!!!) * Many code-...
The user address space is where application code, global variables, per-thread stacks, and DLL code would reside. The system address space is where the kernel, executive, HAL, boot drivers, page ...
11.1. 输出变量的值 (Dumping Variables) 12. Zend_Exception 12.1. 使用“异常” 13. Zend_Feed 13.1. 介绍 13.2. 导入Feeds 13.2.1. 定制 feeds 13.2.1.1. 导入定制的数组 13.2.1.2. 导入定制的数据源 ...
compatibility with older or less security conscience applications. We ; recommending using the production ini in production and testing environments. ; ...
Instantiation using an instance factory method ........................................... 30 4.4. Dependencies ...........................................................................................
Instantiation using an instance factory method ........................................... 30 4.4. Dependencies ...........................................................................................
Table of Contents Preface, Notes, Licenses . . . . . . . . ....1. Licenses for Third-Party Components ....1.1. FindGTest.cmake License ....1.2. LPeg Library License ....1.3. LuaFileSystem Library License ....