【问题标题】:Access value from a Netsuite hash, Enumerator从 Netsuite 哈希访问值,枚举器
【发布时间】:2023-01-04 05:16:21
【问题描述】:

嗨,我正在尝试从自定义字段和其他一些字段中的 Netsuite 哈希中提取值,通常看起来像这样 - `

"custbody_delivery_ticket_number"=>
 {
 "script_id"=>"custbody_delivery_ticket_number", 
 "internal_id"=>"2701", 
 "type"=>"platformCore:DateCustomFieldRef", 
 "attributes"=> {
 "value"=>"123abc"
 }

}` 并希望它的值在属性中。

尝试了许多不同的方法,但特别是一种 -

 delivery_ticket_number: "#{netsuite_sales_orders.custom_field_list.custom_fields.select['custbody_nef_meter_ticket_number']['attributes']['value']}", 

为类 Enumerator 抛出错误,NoMethodError:#Enumerator:0x00005589ec778730 的未定义方法“[]”,这表明可能正在接近,但做错了什么。

如果有人知道如何从这些哈希中获取值?

(系统管理员告诉我这是正确的保管人标识符)

非常感谢

【问题讨论】:

  • select 用于过滤集合,而不是用于访问深度嵌套的哈希内容。如果未提供前者,它需要一个块并返回一个枚举器。看看Hash#digistead。
  • 好的,我尝试了 delivery_ticket_number: "#{netsuite_sales_orders.custom_field_list.custom_fields.dig(:custbody_nef_meter_ticket_number, :attributes, :value) || 'N/A'}", 并且没有将符号隐式转换为整数的错误
  • 它是一个嵌套在散列中的数组,并且有一个名称,例如 delivery_ticket_number: "#{netsuite_sales_orders.dig(:custom_field_list, :custom_fields, 'custbody_nef_meter_ticket_number' , :value)}" 如果可行的话会更好。
  • 与索引一样,它不能保证在同一个地方,因此试图通过名称获取
  • 您可以只发布 netsuite_sales_orders.custom_field_list.custom_fields 的输出吗,因为代码可以正常工作。

标签: ruby ruby-on-rails-3 hash netsuite enumerator


【解决方案1】:

最终解决了这个问题,通过选择和映射获取 Netsuite 自定义字段,如下所示:

delivery_date: netsuite_sales_orders { |field| field.script_id == 'custbody_delivery_date' }.map { |field| field.value }.first

首先通过唯一名称选择 script_id,然后映射到值。 能够获得任何这样的自定义字段,更可取,因为它们可以移动,并且如果使用索引来获取它们,则可能没有相同的索引,从而获取不正确的值。即使项目在自定义字段列表中向上或向下移动,这种方式也能确保获得正确的数据。

避免必须将 custom_fields_list 指定为直接指向 script_id 名称。

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 2014-11-22
    • 2013-04-10
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2017-07-12
    • 2013-03-20
    • 2019-03-19
    • 1970-01-01
    相关资源
    最近更新 更多