我正在使用 group_by 返回一个我想先按键排序的哈希,然后对该键下的值进行排序。
这是我到目前为止提出的代码。一切正常,除了我不知道如何在分组后对值进行排序。
我有一个方法,“pp_accounts_with_properties”,它显示了我的意图并以正确的顺序打印出所有内容(感谢properties.sort_by!调用)......但似乎我应该能够在“accounts_with_properties”中以某种方式完成这一点方法。
class ProofList
attr_reader :client
def initialize(client)
@client = client
end
def properties
@client.properties.joins(invoices: [charges: [:charge_credits]])
.where(charge_credits: { approved: false }).uniq
end
def accounts_with_properties
unsorted_accounts_with_properties.sort_by { |account, properties| account[:acct_number] }
end
def unsorted_accounts_with_properties
properties.group_by { |property| property.account }
end
def pp_accounts_with_properties
accounts_with_properties.each do |account, properties|
puts account.name
properties.sort_by! { |p| p[:name] }
properties.each do |property|
puts property.name
end
end
end
end