2016-12-20 60 views
-1

當我們檢查一個活動記錄結果是否爲空時,會有哪些方法會有更多的性能。帶有效記錄的結果檢查

pc_count = Property.select("id").where('property_category_id = ?', 5).limit(1) 

if pc_count.blank? 
    # 
end 

if pc_count[0] 
    # 
end 

在兩種方式我都試過這種pc_count.blank?pc_count [0],因爲我聽說空白將採取額外的查詢,但是當我試圖在控制檯我看不到任何額外調用

回答

1

沒有,無論你的版本不產生另一個查詢。

這取決於你與pc_count做什麼:

1)您使用的id地方後,然後使用你的方法(我喜歡.blank?

2)您只需要這個爲這個檢查。然後,我會做

Property.select("id").where('property_category_id = ?', 5).limit(1).count 

,因爲不會有創建的模型,你可以測試count == 0