select distinct returns nil (globalize3)
Post.find(:all, :select => "DISTINCT author")
[#<Post author: nil>, #<Post author: nil>, #<Post author: nil>, #<Post author: nil>, #<Post author: nil>]
.
However SQL request SELECT DISTINCT author FROM posts;
works as expected.
I'm using globalize3. Post.select('DISTINCT author').with_translations('en')
returns all records.
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".开发者_如何学C"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)
How can I select only distinct values?
Might be this simple:
Post.find(:all, :group => author)
Update:
Looks like postgres is a little more compliant than mysql, so try this instead:
Post.select("DISTINCT ON (author) *")
精彩评论