获取项目失败后更新XERO项目

时间:2018-01-11 15:01:06

标签: ruby-on-rails xero-api xeroizer

我正在使用xeroizer gem并面临一些问题。

以下是我在控制器中的内容:

items_found = Item.where(code: codes)
if items_found
      xero.Item.batch_save do
      binding.pry
        items_found.each do |existing_item|
          if existing_item.xero_id
            item = xero.Item.find(existing_item.xero_id)
            item.description     = existing_item.description
            item.unit_price      = existing_item.rate
          else
            item = xero.Item.first(where: { code: existing_item.code })
            item.description     = existing_item.description
            item.unit_price      = existing_item.rate
        end
    end
  end
end

items_found数组包含已存储并存在于xero中的所有项目,我从code数组中获取该项目。如果存储了xero_id,我会尝试找到Item,但它会失败。

但是,在binding.pry调用

之后,我的代码因此错误而失败
Xeroizer::ApiException - PostDataInvalidException: The element 'CogsAccountCode' was not recognised. Ensure the element name has the correct case and that there are no duplicate elements of the same name. 
 Generated by the following XML: 
 <ApiException xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ErrorNumber>14</ErrorNumber>
  <Type>PostDataInvalidException</Type>
  <Message>The element 'CogsAccountCode' was not recognised. Ensure the element name has the correct case and that there are no duplicate elements of the same name.</Message>
</ApiException>:
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/http.rb:197:in `handle_error!'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/http.rb:116:in `http_request'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/http.rb:41:in `http_post'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/application_http_proxy.rb:24:in `http_post'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/record/base_model.rb:167:in `block (2 levels) in save_records'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/record/base_model.rb:165:in `block in save_records'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/record/base_model.rb:164:in `save_records'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/record/base_model.rb:191:in `batch_save'
  app/controllers/xero_session_controller.rb:286:in `create_items'

我发现了类似的error

xeroizer gem文档/代码中没有提到CogsAccountCode

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我的codes数组中有一个拼写错误(奇怪的是)没有被抓住并且仍在填充items_found

一旦我修正了codes正确插入的地方,错误就消失了。