0
votes

In my rails customer controller there is an active record collection object:

1. @pay_invoices = Invoice.where(customer_id: @pay_cus_id)

#<ActiveRecord::Relation [#<Invoice id: 37, customer_id: 53, paid_amount: 960, devicemodel_id: 2, transaction_id: "6drv3s", created_at: "2016-01-04 05:29:03", updated_at: "2016-01-25 12:16:14">, #<Invoice id: 70, customer_id: 53, paid_amount: 80, devicemodel_id: 2, transaction_id: "2mr93s", created_at: "2016-01-28 09:02:43", updated_at: "2016-01-28 09:02:43">]>

Also in my controller I am using the transaction_id: column value to find out the transaction details in the Braintree payment gateway using the following Braintree API call:

 @tid = @pay_invoices[0].transaction_id

 @transaction = Braintree::Transaction.find(@tid)

This works fine for me, but the transaction details of the first transaction_id: from the @pay_invoices collection only can be retrieved using this code, I want to iterate through the @pay_invoices collection and each time I want to store the braintree transaction details into another collection, so that only I can display the payment details fro each transaction in my view.

How to achieve it ?

1
Does Braintree's api have something to load multiple transactions at once? - j-dexx
upon each iteration over active collection each time the braitree api call will be made by passing the transaction_id: with it, it returns a collcetion from that I want to store the status of the transaction into somewhere, this process continue till the loop iterate over all active record collection and that many transaction status also to be stored. - Praveen George
What I mean is, does it have an equivalent to ActiveRecord's where clause, where you can pass an array of transaction ids? Something like Braintree::Transaction.where(transaction_id: [1,2,3])? - j-dexx
I dont know about using where clause in Braintree call :O - Praveen George

1 Answers

0
votes
transaction_ids = Invoice.where(customer_id: @pay_cus_id).pluck(:transaction_id)
@transactions = []
transaction_ids.each do |t_id|
  @transactions << Braintree::Transaction.find(t_id)
end

Though if there is a where equivalent you could simply do something like

transaction_ids = Invoice.where(customer_id: @pay_cus_id).pluck(:transaction_id)
@transactions = Braintree::Transaction.where(transaction_id: transaction_ids)