Ruby on rails RubyonRails和SQLite数据库中的列名称不明确?

Ruby on rails RubyonRails和SQLite数据库中的列名称不明确?,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,Ruby On Rails 3.2,我在Rails应用程序中遇到此错误: ActiveRecord::StatementInvalid in PaymentsController#index SQLite3::SQLException: ambiguous column name: date: SELECT COUNT(*) FROM "payments" INNER JOIN "invoices" ON "payments"."invoice_id" = "invoices"."id" WHERE "invoices"."us

我在Rails应用程序中遇到此错误:

ActiveRecord::StatementInvalid in PaymentsController#index

SQLite3::SQLException: ambiguous column name: date: SELECT COUNT(*) FROM "payments" INNER JOIN "invoices" ON "payments"."invoice_id" = "invoices"."id" WHERE "invoices"."user_id" = 1 AND (date >= '2013-01-01' and date <= '2013-12-31')

classpayment=”和date这个答案可能有点模糊,因为您的问题不清楚self.search(year)
方法属于哪一类,但是如果这没有帮助,请告诉我,我们可以尝试进一步

我的猜测是,与方法中的
number\u of\u payments\u不同,在
self.search
方法中,您没有在
where
调用中指定适当的表

中的
number\u of\u payments\u中,您指定了
payments.date
,但在
self.search
中,您只需使用
date
。您说过您的
发票
以及
付款
表中有一个
日期
字段,因此跨这两个表的联接调用都需要对
日期
的引用>以表为范围

self.search
中的
date
前面添加适当的表格(如您在
中的
付款数量中所做的那样)可以解决此问题

class User < ActiveRecord::Base

  def number_of_payments_in(year)
    payments.where("payments.date >= ? and payments.date <= ?", "#{year}-01-01", "#{year}-12-31").count
  end 

end
class Payment < ActiveRecord::Base

  def self.search(year)
    if year
      where("date >= ? and date <= ?", "#{year}-01-01", "#{year}-12-31")
    end
  end

end