I’m using group_by to return a hash that I would like to order by key first, and then order the values under that key.
Here’s the code I’ve come up with so far. Everything works fine, except I can’t figure out how to sort the values after they’re grouped.
I have a method, ‘pp_accounts_with_properties’ which shows my intent and prints out everything in the correct order (thanks to the properties.sort_by! call)… But it seems like I should be able to accomplish this somehow in the ‘accounts_with_properties’ method.
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[:legal_description] }
properties.each do |property|
puts property.name
end
end
end
end
Also, thanks @cpytel for setting me in the right direction w/ the group_by…