Rails 上的未定义方法错误 'find(), find_by(), find_by_id(), where()' Ruby

Undefined Method error 'find(), find_by(), find_by_id(), where()' Ruby on Rails

我是 Ruby Rails 的新手,我正在尝试将一个模型的属性分配给另一个模型。更具体地说,我正在尝试将 Ruby on Rails App 与 Quickbooks Online 集成(使用 Ruckus 的 quickbooks-ruby gem)。但我无法将我的公司(客户)分配给 quickbooks 客户模型。我正在使用 Ruby 2.7.2p137Rails 6.0.3 OAuth 流程已完成并且工作正常。

我收到 Undefined method error 'find' for #<Quickbooks::Model::Customer:0x00007ff178b0ebd8> 错误。我 %100 确定我做错了什么,但我不知道在哪里。我尝试使用此代码将属性分配给 quickbooks 模型(运行 在服务 class btw 下);

    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> 错误。所以我真的被困在这里我不确定到底出了什么问题。谢谢你的时间。

我可以推荐阅读 https://dev.to/adamlombard/ruby-class-methods-vs-instance-methods-4aje

本质上 find_by() find_by_id() where() 是 class 方法。您正在尝试在 class 的实例上调用它们:comp = company.find(i)

在不太了解 quickbook 的情况下,我确定该行需要看起来像这样

Network::Company.find(i)

还值得一提的是,您已经用 companies = Network::Company.all 检索了所有 Network::Company,当您遍历它们时,信息已经可用,您不需要再次检索它们。

因为我们 can see here,gem 中的“模型”不是 ApplicationRecord,所以它没有实现像 find 这样的 ActiveRecord 方法,where, save.

Customer 模型本身 is a parser of XML

如果您想操作数据库操作,我建议您创建自己的 Customer 模型并从 quickbooks gem.

返回的对象构建

第二个代码块中的错误是因为 customer 是一个 Quickbooks::Model::Customer 实例。您当前将其视为 ActiveRecord 模型实例,但事实并非如此。当我们查看 documentation for this class 时,您可以看到那里没有 save 方法。

GitHub readme 为我们提供了您正在寻找的信息:

Updating an object

By default updating an object will un-set any attributes that are NOT specified in the update request. That is, the update is NOT sparse by default. Thus, be careful as you might accidentally unset attributes that you did not specify.

Example:

# fetch a Customer to change their name
customer = service.fetch_by_id("99")
customer.company_name = "Neo Pets"
service.update(customer)

In the above example since we retrieved all fields and then just changed a single attribute, we have given the "complete" entity back to Intuit and effectively only the name is changed.

If you don't have the complete object on hand and only want to change a couple of attributes without un-setting what you are not specifying than you want to use a sparse update:

# update a Customer's name when we only know their ID
customer = Quickbooks::Model::Customer.new
customer.id = 99
customer.company_name = "New Company Name"
service.update(customer, :sparse => true)

因此您应该使用 service.update(customer) 而不是 customer.save 来更新数据。


由于您没有提供有关 Network::Company class 的任何信息,而且它不是“quickbooks-ruby”的一部分 gem 我不能帮助您完成第一个代码块。如果您想解决该问题,则必须提供更多详细信息。