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