... | ... | @@ -5,6 +5,7 @@ MySQL-like group by statement on queries. It keeps only the first row of each se |
|
|
distinct_on(*columns)
|
|
|
```
|
|
|
|
|
|
Just send the list of fields to be included on the `DISTINCT ON` statement:
|
|
|
```ruby
|
|
|
# SELECT DISTINCT ON ( "users"."name" ) "users".* FROM "users"
|
|
|
User.distinct_on(:name).all
|
... | ... | @@ -12,6 +13,9 @@ User.distinct_on(:name).all |
|
|
|
|
|
You can use where-like syntax to find more complex columns:
|
|
|
```ruby
|
|
|
# SELECT DISTINCT ON ( "users"."name" ) "users".* FROM "users"
|
|
|
User.distinct_on(:name).all
|
|
|
# SELECT DISTINCT ON ( "photos"."type" ) "users".* FROM "users" INNER JOIN "photos" ON "users"."id" = "photos"."user_id"
|
|
|
User.joins(:photos).distinct_on(photos: :type).all
|
|
|
|
|
|
# SELECT DISTINCT ON ( "photos"."type", "photos"."size" ) "users".* FROM "users" INNER JOIN "photos" ON "users"."id" = "photos"."user_id"
|
|
|
User.joins(:photos).distinct_on(photos: [:type, :size]).all
|
|
|
``` |
|
|
\ No newline at end of file |