jeudi 28 janvier 2016

Rails 4 : Active record collection looping or iteration

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 ?

Aucun commentaire:

Enregistrer un commentaire