Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7416239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:27:15+00:00 2026-05-29T07:27:15+00:00

I am trying to construct a site which ranks performances for a selection of

  • 0

I am trying to construct a site which ranks performances for a selection of athletes in a particular event – I have previously posted a question which received a few good responses which me to identify the key problem with my code currently.

I have 2 models – Athlete and Result (Athlete HAS MANY Results)

Each athlete can have a number of recorded times for a particular event, i want to identify the quickest time for each athlete and rank these quickest times across all athletes.

I use the following code:

 <% @filtered_names = Result.where(:event_name => params[:justevent]).joins(:athlete).order('performance_time_hours ASC').order('performance_time_mins ASC').order('performance_time_secs ASC').order('performance_time_msecs ASC') %>

This successfully ranks ALL the results across ALL athletes for the event (i.e. one athlete can appear a number of times in different places depending on the times they have recorded).

I now wish to just pull out the best result for each athlete and include them in the rankings. I can select the time corresponding to the best result using:

 <% @currentathleteperformance = Result.where(:event_name => params[:justevent]).where(:athlete_id => filtered_name.athlete_id).order('performance_time_hours ASC').order('performance_time_mins ASC').order('performance_time_secs ASC').order('performance_time_msecs ASC').first() %>

However, my problem comes when I try to identify the distinct athlete names listed in @filtered_names. I tried using <% @filtered_names = @filtered_names.select('distinct athlete_id') %> but this doesn’t behave how I expected it to and on occasions it gets the rankings in the wrong order.

I have discovered that as it stands my code essentially looks for a difference between the distinct athlete results, starting with the hours time and progressing through to mins, secs and msec. As soon as it has found a difference between a result for each of the distinct athletes it orders them accordingly.

For example, if I have 2 athletes:

Time for Athlete 1 = 0:0:10:5
Time for Athlete 2 = 0:0:10:3

This will yield the order, Athlete 2, Athlete1

However, if i have:

Time for Athlete 1 = 0:0:10:5
Time for Athlete 2 = 0:0:10:3
Time for Athlete 2 = 0:1:11:5

Then the order is given as Athlete 1, Athlete 2 as the first difference is in the mins digit and Athlete 2 is slower…

Can anyone suggest a way to get around this problem and essentially go down the entries in @filtered_names pulling out each name the first time it appears (i.e. keeping the names in the order they first appear in @filtered_names

Thanks for your time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:27:17+00:00Added an answer on May 29, 2026 at 7:27 am

    If you’re on Ruby 1.9.2+, you can use Array#uniq and pass a block specifying how to determine uniqueness. For example:

    @unique_results = @filtered_names.uniq { |result| result.athlete_id }
    

    That should return only one result per athlete, and that one result should be the first in the array, which in turn will be the quickest time since you’ve already ordered the results.

    One caveat: @filtered_names might still be an ActiveRecord::Relation, which has its own #uniq method. You may first need to call #all to return an Array of the results:

    @unique_results = @filtered_names.all.uniq { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so I have this web site search script and I'm trying to count
I have a site a client is trying to use that doesnt work on
Am trying to construct a simple update query in my model class Model_DbTable_Account extends
I'm trying to construct a find command to process a bunch of files in
I'm trying to construct a query that will include a column indicating whether or
I am trying to construct a way to keep certain hard drive partitions/usb drives
I'm trying to construct a contrapositive for the following statement: If A is 0
I am trying to construct a TreeView from a Menu. My Code is like
I am trying to construct a Groovy statement to find values that don't exist
I'm currently trying to construct a list of bean classes in Java from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.