我很困惑在哪裏使用'.find'以及在哪裏使用'where'。在執行查詢期間性能是否存在差異?。查找或在有效記錄查詢條件
例如:轉換後的現有查詢被如下使用.find:
FileOrFolder.find_by_fullpath(completePath, :select=>"id")
- >
FileOrFolder.where(fullpath: completePath).select(:id).first
Component.find(:first, :conditions=>["cluster_id = ? AND name = ?", cluster_id, key])
- >
Component.where(cluster_id: cluster_id, name: key).first
在性能上沒有差異 - 同樣的查詢,但更「得心應手」使用'where'因爲返回的ActiveRecord ::的關係(http://stackoverflow.com/questions/9574659/rails-where-vs-find) – MrYoshiji
@MrYoshiji ...我已經通過這篇文章....沒有它談到查詢性能的權利... ..pls糾正我,如果我錯了 – Vinay
你可以通過在你的控制檯'Component.where(id:1).first'和'Component.find(1)'中測試你自己來做,你會發現它產生了EXACT相同的查詢,並採取同一時間,因爲它們是...相同的查詢! – MrYoshiji