Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ambiguous error for created_at on joins query

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading