lawrence.adu
lawrence.adu

Reputation: 89

Matching Records at database level

I'm using Ruby on rails 5 and I have a table called Transaction(reference,id). This table has nth transactions but at each point, at most 2 transactions have the same reference number but different ids. How do I match or reconcile the two transactions with matching reference or pair them in one select row at the database level?(tried using loops, was too slow for large transactions). Using postgresql

Upvotes: 0

Views: 46

Answers (1)

Sachin R
Sachin R

Reputation: 11876

Use group by

Transaction.group_by(&:reference).each do |reference, transaction|
  p "#{reference} -> #{transaction.map(&:class).join(', ')}"
end

output like this 
"#reference number -> Transaction"
"#reference number -> Transaction"
"#reference number -> Transaction, Transaction"

Try this

Upvotes: 1

Related Questions