Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid limit parameter ignored

I tried to grab the latest N records with a unique value (first_name).

So far:

@users = User.all(:limit => 5, :sort => [:created_at, :desc]).distinct(:first_name)

almost works..But ignores the limit and sort order
Also:

@users = User.limit(5).desc(:created_at).distinct(:first_name)

Ignores both 'limit' and 'desc'

@users = User.limit(5)

Works..

What am I doing wrong?
Any help would be greatly appreciated!

like image 454
Lamp Avatar asked Apr 02 '26 07:04

Lamp


1 Answers

I played with this for a little while and this is the best I could come up with.

Good luck.

@users = User.desc(:created_at).reduce([]) do |arr, user|
  unless arr.length == 5 || arr.detect{ |u| u.first_name == user.first_name }
    arr << user
  end
  arr
end
like image 98
Kyle Avatar answered Apr 04 '26 03:04

Kyle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!