2010-05-07 39 views
0
unless scope.nil? 
    @page ||= Page.find(id, :scope => Page.find(scope)) 
else 
    @page ||= Page.find(id) 
end 
+2

在未来,请四个空格等堆栈溢出所示,当它实际上看起来像'code'缩进代码。 – 2010-05-07 14:36:41

回答

3
@page ||= Page.find id, :scope => (Page.find scope if scope) 
+0

这当然是最好的了,正如最后提到的那样,它当然是优先考虑的。 – ktec 2010-05-11 15:38:01

0

你可以这样做:

@page ||= unless scope.nil? 
    Page.find(id, :scope => Page.find(scope)) 
else 
    Page.find(id) 
end 
0

或者:

@page ||= scope.nil? ? Page.find(id) : Page.find(id, :scope => Page.find(scope)) 
1

我会写像以下问题的块。它真的归结为偏好,但我觉得这种方式是最可读的。

@page ||= 
    if scope 
    Page.find id, :scope => Page.find(scope) 
    else 
    Page.find id 
    end 
2

这是一个位机:

find_opts = scope.nil? ? {} : {:scope => Page.find(scope)} 
@page ||= Page.find(id, find_opts) 
相关问题