【发布时间】:2014-11-13 04:59:21
【问题描述】:
假设您有一个模型 Dog,其属性为“年龄”,这是一种确定“吠叫频率”的方法:
class Dog < ActiveRecord::Base
scope by_age, -> { order('age ASC') }
def barking_frequency
# some code
end
end
在您的控制器中,您有:
def index
@dogs = Dog.by_age
end
一旦有了@dogs 的哈希值,如何按 barking_frequency 对其进行排序,以便结果将特定年龄的狗排序在一起,如下所示:
Name Age Barking Frequency
Molly 2 1
Buster 2 4
Jackie 2 7
Dirk 3 1
Hank 3 3
Jake 3 4
Spot 10 0
【问题讨论】:
标签: ruby-on-rails ruby sorting hash