Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41618
  • 博文数量: 27
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-24 00:44
文章分类
文章存档

2011年(1)

2009年(26)

我的朋友
最近访客

分类: Java

2009-04-17 12:43:04

  1. class ApplicationController < ActionController::Base  
  2.     def current_user  
  3.         @current_user ||= User.find(session[:user_id])  
  4.     end  
  5. end
this piece of codes are much better than the following pieces:

  1. class ApplicationController < ActionController::Base  
  2.     def current_user  
  3.         User.find(session[:user_id])  
  4.     end  
  5. 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) |
0

上一篇:没有了

下一篇:Dynamic find_by Methods

给主人留下些什么吧!~~