Ruby on rails RubyonRails和SQLite数据库中的列名称不明确?
我在Rails应用程序中遇到此错误: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
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