Reputation: 1084
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?
Upvotes: 4
Views: 4835
Reputation: 22258
@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)
Upvotes: 11