![](/img/trans.png)
[英]Complex Rails Active Record query to select record with true result for that day else latest updated else created record for day
[英](Rails Active Record): Request with constraint for uniqueness of a filed of the result objects
好的,我得到了以下簡單的模型類:
class Baby < ActiveRecord::Base
attr_accessible :name, :born_at
...
end
而且我需要收集最年輕的20個嬰兒,這些嬰兒有着獨特的名字
Baby.all(:order => "born_at desc", :limit => 20)
但我不知道要求添加什么,所以嬰兒的名字是獨一無二的。
免責聲明:我對SQL數據庫很新,所以不要因為我的蹩腳問題來判斷我。
Baby.group(:name).order('born_at desc').limit(20)
Baby.all(:order => 'born_at desc', :limit => 20, :group => :name)
或者如果您只需要名字
Baby.select(:name).limit(20).order('born_at desc')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.