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

  • SEARCH
  • Home
  • 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 6053043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:59:42+00:00 2026-05-23T07:59:42+00:00

I have a ranking which shows the fastest User: @users = User.find.sort { |a,

  • 0

I have a ranking which shows the fastest User:

@users = User.find.sort { |a, b| b.finished_at <=> a.created_at }

Now I hat to add some code to prevent getting an error because of finished_at beeing nil until the user finished.

@users = User.find(:all, :conditions => "finished_at IS NOT NULL").sort { |a, b| b.finished_at <=> a.created_at }

But sort doesn’t work. Is this way of chaining methods flawed? And is find(:all, :conditions => "finished_at IS NOT NULL") the rails 3 way or outdated?

Thanks in advance

  • 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-23T07:59:42+00:00Added an answer on May 23, 2026 at 7:59 am

    The rails 3 way to query this would be:

    @users = User.where('finished_at IS NOT NULL') 
    

    As for the sort, yes, it’s ok to chain like that. A simple sort, where it was all on a single field, would be better in sql:

    @users = User.where('finished_at IS NOT NULL').order(:finished_at)
    

    However, your sort does something weird, comparing one field on the left and another on the right. I don’t think your sort algorithm is going to work right, as it will depend on the initial record order. Since you are comparing apples to oranges, it’s not going to be consistent.

    You said “the fastest user”, which makes me think you really want the time difference between the created_at and finished_at times:

    @users = User.where('finished_at IS NOT NULL').
                  sort {|a,b| (a.finished_at - a.created_at) <=> (b.finished_at - b.created_at)
    

    If you want to let rails 3 lazy load that, however, you have to sort in the sql, instead of in ruby. This should work in mysql I think:

    @users = User.where('finished_at IS NOT NULL').
                  select('users.*, timediff(finished_at,created_at) AS duration').
                  order('duration')
    

    This way, the query won’t be executed until values are actually called for, which helps if you need page caching. You also have the benefit of having the duration available:

    @users[0].duration
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForms application, and wanted to add some nice WPF controls, one
I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
I'm designing a Java application using SWT. I have some tables which will display
HI, I have the following table which save agent ranking on daily basis on
I have a Competitions results table which holds team member's names and their ranking
I have this SQL Query which should find the rank of a certain cook.
I have a website that has user ranking as a central part, but the
I have a Django application in which I need to implement a simple trending/ranking
i have a large mysql database table in which one column contains values ranging
I have a huge database which holds pairs of numbers (A,B), each ranging from

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.