0
我正在使用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
我只是排序進行顯示。但是我必須在幾個地方這樣做,所以我想我是否可以在生成時做到這一點,然後每次顯示數據時都可以避免將其分類。但是,我明白了您的觀點,試圖使用該命令做任何操作數據的操作。 –