select distinct返回nil(globalize3)

时间:2011-07-19 13:03:09

标签: sql ruby-on-rails postgresql activerecord

Post.find(:all, :select => "DISTINCT author")
返回
[#<Post author: nil>, #<Post author: nil>, #<Post author: nil>, #<Post author: nil>, #<Post author: nil>]

但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)

如何只选择不同的值?

1 个答案:

答案 0 :(得分:0)

可能很简单:

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

更新

看起来postgres比mysql更合规,所以试试吧:

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