这是困扰我。它看起来不太干。什么是更好的实施?顺便说一句,当没有找到记录时,这个ActiveRecord finder怎么会不会引发异常,但是.find呢?如何干这段Ruby代码?
def current_account
return @account if @account
unless current_subdomain.blank?
@account = Account.find_by_host(current_subdomain)
else
@account = nil
end
@account
end
+1,你比我好多了。 – 2009-10-21 17:05:09
但是,如果current_subdomain是空字符串,则不应调用.find_by_host。 如果&&失败,将分配什么@account?假? – Alexandre 2009-10-21 17:41:38
但是,如果current_subdomain是“”,则会失败。 “”在布尔上下文中评估为true。应该是'!current_subdomain.blank?' – EmFi 2009-10-21 20:37:57