2014-04-29 56 views
0

Category有很多Post對於某個屬性設置爲零且最後一個未設置爲零的對象的查詢

我想(的AssociationRelation類)與集合:

具有屬性published_at設置爲nil

連同

後與最近的「published_at」日期的所有帖子

另外這兩個查詢看起來像下面這樣:
.where("published_at is NULL")
.order(published_at: :desc).limit(1)

但我怎麼能有一個集合,這是兩者的結果?

回答

0

使用partition

recent, rest = Post.order('published_at DESC').partition{ |e| e[:published_at] != nil } 
recent_one = Array.new << recent.first 
@posts = recent_one + rest 

所以,在@posts第一個職位將是最近一次公佈的職位,其餘的將與published_atnil

+0

第二行是給我:'未定義的方法\ '+'for#' – Numbers

+0

但是你的'+'方法給了我這個想法,簡單地將我的兩個發佈的查詢的結果添加到eachother,它似乎工作! :) – Numbers

+0

是的,看到我的edites答案,錯誤是因爲'recent.first'不是一個數組,所以將其轉換爲數組,然後添加將解決問題,這也在一個單一的查詢:) – RSB

相關問題