vendredi 1 juillet 2022

How can you use distinct in rails while still using ActiveRecord's

I am struggling with the following problem: I want to have two different tabs, one that displays all recent chugs (Done), and one that displays the chugs that are the fastest per person.

However, this needs to remain an ActiveRecord, since I need to use it with link_to and gravatar, thus restraining me from group_by, as far as I understand it.

AKA: If there are three users who each have three chugs, I want to show 1 chug per user, which contains the fastest time of that particular user.

The current code looks like this, where chugs_unique should be edited:

  def show
    @pagy, @chugs_all_newest = pagy(@chugtype.chugs.order('created_at DESC'), items: 10, page: params[:page])
    @chugs_unique = @chugtype.chugs.order('secs ASC, milis ASC, created_at DESC').uniq

    breadcrumb @chugtype.name, chugtypes_path(@chugtype)
  end

In this case, a chug belongs to both a chugtype and user, and the chugtype has multiple chugs.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire