2011-07-19 108 views
1

Post.find(:all, :select => "DISTINCT author")
return
[#<Post author: nil>, #<Post author: nil>, #<Post author: nil>, #<Post author: nil>, #<Post author: nil>]select distinct returns nil(globalize3)

但SQL請求SELECT DISTINCT author FROM posts;按預期工作。

我正在使用globalize3。 Post.select('DISTINCT author').with_translations('en')返回所有記錄。
下面是它的日誌中:

Post Load (0.5ms) SELECT "posts"."id" AS t0_r0, "posts"."description" AS t0_r1, "posts"."position" AS t0_r2, "posts"."created_at" AS t0_r3, "posts"."updated_at" AS t0_r4, "posts"."date" 
AS t0_r5, "post_translations"."id" AS t1_r0, "post_translations"."post_id" AS t1_r1, "post_translations"."locale" AS t1_r2, "post_translations"."author" 
AS t1_r3, "post_translations"."description" AS t1_r4, "post_translations"."created_at" AS t1_r5, "post_translations"."updated_at" 
AS t1_r6 FROM "posts" LEFT OUTER JOIN "post_translations" ON "post_translations"."post_id" = "posts"."id" WHERE "post_translations"."locale" = 'en' 
AND (post_translations.author IS NOT NULL) 

我怎麼可以只選擇不同的值?

+0

請發表日誌塊用兩個請求 – Bohdan

+0

已更新。這是第一個請求:'ActionView :: Template :: Error(No route matches {:action =>「show」,:controller =>「posts」,:id =>#}):' – NARKOZ

回答

0

可能是這個簡單:

Post.find(:all, :group => author) 

更新:

貌似Postgres的是一個小比MySQL更柔順,所以嘗試這個:

Post.select("DISTINCT ON (author) *") 
+0

'Post load(0.5ms)SELECT「posts」。* FROM「posts」WHERE(post_category_id = 1)GROUP BY作者 PGError:錯誤:列「posts.id」必須出現在GROUP BY子句中或用於彙總函數 LINE 1:SELECT「posts」。* FROM「posts」WHERE(post_category_id = 1)G ...' – NARKOZ

+0

增加了postgres解決方案,修復了由bug –

+0

thx組的bug, – NARKOZ