Mongological inquiry! Combination of limit and order

In my rails application, I want to get the last 10 entries with a unique value.

I tried:

@users = User.all(:limit => 10, :sort => [:created_at, :desc]).map{|t| t.first_name}.uniq 

but

the limit does not work as I expect.

I want "limit" to execute the last request.

any ideas?

+4
source share
1 answer
 @users = User.desc(:created_at).limit(10).map(&:first_name).uniq 

You must use distinct . It is possible that uniq will result in less than 10 entries.

 @users = User.limit(10).distinct(:first_name).desc(:created_at).map(&:first_name) 
+11
source

Source: https://habr.com/ru/post/1401086/


All Articles