【发布时间】:2021-08-11 09:21:41
【问题描述】:
我是 Ruby on Rails 的新手,我正在尝试将属性从一个模型分配给另一个模型。更具体地说,我正在尝试将我的 Ruby on Rails 应用程序与 Quickbooks Online 集成(使用 Ruckus 的quickbooks-ruby gem)。但是我不能将我的公司(客户)分配给 quickbooks 客户模型。我正在使用Ruby 2.7.2p137 和Rails 6.0.3 OAuth 过程已完成并且工作正常。
我收到Undefined method error 'find' for #<Quickbooks::Model::Customer:0x00007ff178b0ebd8> 错误。我 %100 确定我做错了什么,但我不知道在哪里。我尝试使用此代码为 quickbooks 模型分配属性(顺便说一句,在服务类下运行);
def qb_customers
set_client #For setting the OAuth client works fine.
service = Quickbooks::Service::Customer.new
service.access_token = @access_token
service.company_id = @options[:realm_id]
companies = Network::Company.all
customer = Quickbooks::Model::Customer.new
i = 0
companies.each do |company|
comp = company.find(i)
if comp.name == customer.display_name
i += 1
else
customer.display_name = comp.name
customer.primary_phone&.free_form_number = comp.tel
customer.mobile_phone&.free_form_number = comp.gsm
customer.fax_phone&.free_form_number = comp.fax
customer.primary_email_address&.address = comp.email
customer.web_site&.uri = comp.website
customer.billing_address&.line1 = comp.address
customer.billing_address&.city = comp.city
customer.billing_address&.country = comp.state
customer.billing_address&.postal_code = comp.postcode
customer.billing_address&.country_sub_division_code = comp.country_id
customer.billing_address&.lat = comp.latitude
customer.billing_address&.lon = comp.longtude
customer.notes = comp.notes
customer.save
end
end
customers = service.query(nil, :page => 1, :per_page => 900000)
end
用find_by()、find_by_id()、where() 也试过这个块,但它们都返回了未定义的方法错误。
所以我尝试了不同的方法;
companies.each do |comp|
if comp.name != customer.display_name
customer.display_name = comp.name
customer.primary_phone&.free_form_number = comp.tel
#same attributes here
.
.
.
customer.notes = comp.notes
customer.save
else
next
end
end
这次我收到了undefined method 'save' for #<Quickbooks::Model::Customer:0x00007ff178b0ebd8> 错误。所以我真的被困在这里我不确定到底出了什么问题。感谢您的宝贵时间。
【问题讨论】:
-
这个问题有些可疑。 "Undefined method error 'find' for #<:model::customer:0x00007ff178b0ebd8>" 建议您在
Quickbooks::Model::Customer实例上调用find,但在在Network::Company.all结果(companies)上调用问题(第一个代码块),我假设它返回 ActiveRecord 关系,而不是Quickbooks::Model::Customer实例。 -
我按原样再次运行它,它给出了
undefined method 'find' for #<Network::Company:0x00007fdbf973d6b0>糟糕,我记得,我用“保存”复制了底部错误,然后将其更改为查找。我认为这不会是一个问题,因为两者都是未定义的方法错误,但是是的,我对此不小心抱歉。 -
另外,您在迭代之外实例化新的
Quickbooks::Model::Customer...这只会用下一个覆盖客户的先前数据... -
什么是
Network::Company?你自己定义的吗?它是由其他宝石定义的吗?看起来它不是 ActiveRecord 模型,因为在您之前的评论中您说Network::Company.all返回#<Network::Company:0x00007fdbf973d6b0>而 Rails 模型将返回 ActiveRecord 关系。 -
第二个代码块的错误比较明显。
Quickbooks::Model::Customer实例没有save方法(它不是 Rails 模型实例)。 GitHub 上的自述文件说您应该使用service.update(customer)更新您的对象。
标签: ruby-on-rails ruby quickbooks-online