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?
@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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With