我想通過它的:id
找到Blog
,然後select
只有它的幾個屬性。Rails查詢:按ID查找第一條記錄並僅選擇該記錄的指定屬性
實施例:
# Doesn't work and I don't know why
# I want to grab the first blog with this id, and then grab only selected attributes
blog = Blog.find(10).select(:id, :title, :date)
This post建議使用where
代替find
。但據我所知,where
在我的場景中是效率低下的,因爲它繼續在數據庫中搜索更多匹配,即使它發現我想抓取的唯一記錄。
我該如何指定抓住它找到的第一條記錄,然後只抓取選定的屬性?
啊!我想訂單在這裏很重要。謝謝。 – Neil
歡迎您! – nsave