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 7680527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:08:03+00:00 2026-05-31T18:08:03+00:00

I’m building a controller/view that provides a wide selection of player rankings (e.g. Top

  • 0

I’m building a controller/view that provides a wide selection of player rankings (e.g. “Top 10 Leader Boards”.) Using this model:

class Player < ActiveRecord::Base
  attr_accessible :name, :games_played, :games_lost, :games_won, games_exited, 
                  :total_kills, :total_deaths, :total_points, :total_coins
end

In my controller I have some obvious query results to pass to my view to populate player ranking lists:

@top_winners = Player.order("games_won DESC").limit(10)
@top_assassins = Player.order("total_kills DESC").limit(10)

I now need to add some sorted rankings that are calculations. Examples:

@most_greedy would be sorted on:      :total_coins / :games_played
@most_lethal would be sorted on:      :total_kills / :games_played
@most_vanquished would be sorted on:  :total_deaths / (:games_lost + :games_exited)

My approach is to get all the players in an array and then use Ruby’s array.sort {| a,b | block } → new_array option. In the case of @most_greedy I tried this:

rich_players = Player.order("total_coins DESC").limit(30)  # only consider top 30 richest
@most_greedy = rich_players.sort {|total_coins, games_played| x / y }.slice(0, 9)

Which generates the error:

undefined local variable or method `x' for #<PlayersController:0x007fb7dac59d08>

Unfortunately my meager AR understanding and Ruby skills are failing me. How can I make this approach work? Is there a different approach to this type of problem? I didn’t see anything in the AR Query Guide like this.

  • 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-31T18:08:05+00:00Added an answer on May 31, 2026 at 6:08 pm

    sort is not active record, it is plain old ruby, and uses a block with two parameters to compare both objects, which are going to be Player objects.

    @most_greedy = rich_players.sort {|x, y| 
      (x.total_coins / x.games_played)  <=>  (y.total_coins / y.games_played)
    }.slice(0, 9)
    

    Or even better, using sort_by:

    @most_greedy = rich_players.sort_by {|x|
      x.total_coins / x.games_played
    }.slice(0, 9)
    

    If you want to use the database to calulate (which might give different results, ie, may find a less weathly player that has a better score than by limiting to the 10 top wealthy players) you might try this… (untested)

    @most_greedy = Player.select('*, total_coins/games_played as greediness').order('greediness DESC').limit(10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.