ActiveRecord :: Relation print特定属性

时间:2015-10-20 17:36:05

标签: ruby-on-rails activerecord

我之间有一个ActiveRecord :: Relation:

订单类:

has_many :transactions, :class_name => "OrderTransaction"

OrderTransaction类:

belongs_to :order    
serialize :params
  

ActiveRecord :: Relation [#OrderTransaction id:5,order_id:11,action:“purchase”,金额:26044,成功:true,授权:“8V863650823585301”,消息:“Success”,params:{“timestamp” =>“2015-10-16T23:11:09Z”,“ack”=>“成功”,“correlation_id”=>“9b5eb7051dbe3”,“版本”=>“72”,“build”=> ;“18308778”,“金额”=>“260.44”,“amount_currency_id”=>“USD”,“avs_code”=>“X”,“cvv2_code”=>“M”,“transaction_id”=> ;“8V863650823585301”,“Timestamp”=>“2015-10-16T23:11:09Z”,“Ack”=>“成功”,“CorrelationID”=>“9b5eb7051dbe3”,“版本”=>“ 72“,”Build“=>”18308778“,”金额“=>”260.44“,”AVSCode“=>”X“,”CVV2Code“=>”M“,”TransactionID“=>” 8V863650823585301“},created_at:”2015-10-16 23:11:09“,updated_at:”2015-10-16 23:11:09“]

我正在尝试在irb控制台中打印params [:transaction_id],但我找不到正确的方法来执行此操作。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这应该这样做:

OrderTransaction.where(message: "Success").map { |p| p['params']['transaction_id'] }

只获得一个transaction_id

x = OrderTransaction.where(message: "Success").last
x['params']['transaction_id']