Home > Blockchain >  Ambiguous error for created_at on joins query
Ambiguous error for created_at on joins query

Time:10-04

I have an active record query that is causing an ambiguous error for created_at. The query is:

Payment.joins(:invoice).successful.where('created_at < ?', 1.hour.ago)

The is aiming to return any successful payments that have invoices where the payment created at is older than an hour. However due to the .joins(:invoice), it is getting confused about the 'created_at' scope as both invoice and payment have a created_at column.

An alternative i have tried is: Payment.joins(:invoice).successful.where(created_at: *code for older than an hour ago that im unsure on*) or Payment.joins(:invoice).successful.where('self.created_at < ?', 1.hour.ago) however i can't seem to get the syntax correct.

Is there any way to solve this? Thank you.

CodePudding user response:

You can prefix the column with the table name, in this case payments;

Payment
  .joins(:invoice)
  .successful
  .where('payments.created_at < ?', 1.hour.ago)

Notice that your attempt to use self won't work because whatever you pass to where as a string is "interpreted" as raw SQL - what they refer to as "a SQL fragment". self is a Ruby keyword with no use in your RDBMS.

  • Related