【问题标题】:Rails 4 : Active record collection looping or iterationRails 4:活动记录收集循环或迭代
【发布时间】:2016-05-05 14:08:36
【问题描述】:

在我的 rails 客户控制器中有一个活动记录集合对象:

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">]>

另外,在我的控制器中,我使用 transaction_id: 列值通过以下 Braintree API 调用找出 Braintree 支付网关中的交易详情:

 @tid = @pay_invoices[0].transaction_id

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

这对我来说很好,但是只能使用此代码检索 @pay_invoices 集合中第一个 transaction_id: 的交易详细信息,我想遍历@pay_invoices 集合,并且每次我想将 Braintree 交易详情存储到另一个集合中,这样只有我才能在我的视图中显示每笔交易的付款详情。

如何实现?

【问题讨论】:

  • Braintree 的 api 是否有东西可以一次加载多个事务?
  • 在每次对活动集合进行迭代时,每次将通过传递 transaction_id 进行 braitree api 调用:使用它,它会返回一个集合,我想将事务的状态存储到某个地方,这个过程继续进行,直到循环遍历所有活动记录集合并且还要存储许多事务状态。
  • 我的意思是,它是否有等价于ActiveRecordwhere 子句,您可以在其中传递一组交易ID? Braintree::Transaction.where(transaction_id: [1,2,3]) 之类的东西?
  • 我不知道在 Braintree 调用中使用 where 子句:O

标签: ruby-on-rails braintree


【解决方案1】:
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

虽然如果有一个等价的地方,你可以简单地做类似的事情

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

【讨论】:

  • 感谢哥们的帮助,过一段时间我会试试,如果它解决了我的问题,请告诉你:)
猜你喜欢
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多