How do I get unique elements in this array?

前端 未结 6 1064
时光取名叫无心
时光取名叫无心 2021-01-30 06:11

Using Mongoid. Unfortunately, Mongoid does not allow for selecting unique / distinct! Have gotten these results. As you can see, there are 7 results. If you look carefully (at

6条回答
  •  逝去的感伤
    2021-01-30 06:47

    Errr, it's a bit messy in the view. But I think I've gotten it to work with group (http://mongoid.org/docs/querying/)

    Controller

    @event_attendees = Activity.only(:user_id).where(:action => 'Attend').order_by(:created_at.desc).group
    

    View

    <% @event_attendees.each do |event_attendee| %>    
      <%= event_attendee['group'].first.user.first_name %>
    <% end %>
    

提交回复
热议问题