Reputation: 10207
I am getting this error in my Rails app:
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')
The problem seems to be that I have a date
field in my invoices
as well as my payments
table.
Yet I still don't know how to fix this error.
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
Can anybody help?
Upvotes: 0
Views: 618
Reputation: 524
This answer may be a bit vague as it's not clear from your question which class the self.search(year)
method is in, but let me know if this doesn't help and we can try and go further.
My guess is that, unlike in the number_of_payments_in
method, in the self.search
method you haven't specified an appropriate table in the where
call.
In number_of_payments_in
, you specify payments.date
, but in self.search
you just use date
. You said that you have a date
field in your invoices
as well as your payments
table, so a join call across both tables will need every reference to date
to be scoped by table.
Adding the appropriate table in front of date
in self.search
(as you have done in number_of_payments_in
) may solve the problem.
Upvotes: 2