我正在构建一个控制器/视图,提供多种玩家排名(例如“十大领袖板”)。使用此模型:
class Player < ActiveRecord::Base
attr_accessible :name, :games_played, :games_lost, :games_won, games_exited,
:total_kills, :total_deaths, :total_points, :total_coins
end
在我的控制器中,我有一些明显的查询结果传递到我的视图以填充玩家排名列表:
@top_winners = Player.order("games_won DESC").limit(10)
@top_assassins = Player.order("total_kills DESC").limit(10)
我现在需要添加一些计算的排序排名。例子:
@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)
我的方法是让所有玩家进入阵列,然后使用Ruby的array.sort {| a,b | block } → new_array
选项。在@most_greedy
的情况下,我尝试了这个:
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)
哪会产生错误:
undefined local variable or method `x' for #<PlayersController:0x007fb7dac59d08>
不幸的是,我微弱的AR理解和Ruby技能让我失望。我怎样才能使这种方法有效?这类问题有不同的方法吗?我在AR查询指南中没有看到这样的内容。
答案 0 :(得分:2)
sort不是活动记录,它是普通的旧ruby,并使用带有两个参数的块来比较两个对象,这些对象将成为Player对象。
@most_greedy = rich_players.sort {|x, y|
(x.total_coins / x.games_played) <=> (y.total_coins / y.games_played)
}.slice(0, 9)
甚至更好,使用sort_by:
@most_greedy = rich_players.sort_by {|x|
x.total_coins / x.games_played
}.slice(0, 9)
如果你想使用数据库进行计算(这可能会产生不同的结果,也就是说,可能会找到一个不那么好的玩家,其分数比限制在10个顶级富有玩家中更好),你可以试试这个...(未测试的)
@most_greedy = Player.select('*, total_coins/games_played as greediness').order('greediness DESC').limit(10)