-1

我在 ActiveRecord 3.2.8 中遇到了奇怪的错误,代码如下:

我有 2 个模型 - 客户和发票。

class Customer < ActiveRecord::Base
  has_many :invoices
end

class Invoice < ActiveRecord::Base
  belongs_to :customer

  def self.last(customer)
    customer.invoices.order("some_date_column DESC").first
  end
end

我正在运行与此类似的代码:

Thread.new do
  ActiveRecord::Base.connection_pool.with_connection do
    Customer.all.each do |customer|
      invoice = Invoice.last(customer)
      ...
    end
  end
end

运行该代码时,有时会出现以下奇怪错误:

/var/lib/gems/1.8/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:25:in 'initialize': wrong number of arguments (1 for 0) (ArgumentError)
from /var/lib/gems/1.8/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:25:in 'new'
from /var/lib/gems/1.8/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:25:in 'initialize'
from /var/lib/gems/1.8/gems/activerecord-3.2.8/lib/active_record/associations.rb:157:in 'new'
from /var/lib/gems/1.8/gems/activerecord-3.2.8/lib/active_record/associations.rb:157:in 'association'
from /var/lib/gems/1.8/gems/activerecord-3.2.8/lib/active_record/associations/builder/association.rb:44:in 'invoices'
from .../models/invoice.rb:7:in 'last'

任何帮助深表感谢。

4

1 回答 1

1

当你遇到这种错误时

wrong number of arguments (X for Y)

这意味着您试图调用一个传递与预期不同数量的参数的方法。在您的情况下,您调用的方法不应该接收任何参数(for 0),但您尝试传递一个(1 for)。从错误的最后一行来看,问题似乎是Invoice.last(customer)

于 2012-09-27T00:59:34.417 回答