Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid query! Combination of limit and order

In my rails app I want to grab the 10 last records with a unique value.

I tried:

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

but,

the limit doesn't work as I expect..

I want the 'limit' to effect the query last.

any ideas?

like image 371
Lamp Avatar asked Dec 21 '22 00:12

Lamp


1 Answers

@users = User.desc(:created_at).limit(10).map(&:first_name).uniq

You should use distinct. It is possible that uniq will cause you to get fewer than 10 records.

@users = User.limit(10).distinct(:first_name).desc(:created_at).map(&:first_name)
like image 105
Kyle Avatar answered Jan 15 '23 15:01

Kyle