Created
February 7, 2017 12:51
-
-
Save tmikeschu/e31f8db0567e63535982548a6c571d78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Merchant < ApplicationRecord | |
has_many :items | |
has_many :invoices | |
has_many :invoice_items, through: :invoices | |
has_many :transactions, through: :invoices | |
has_many :customers, through: :invoices | |
def customers_with_pending_invoices | |
Customer.find_by_sql("select customers.* from customers | |
join invoices | |
on customers.id = invoices.customer_id | |
where invoices.id in ( | |
select invoices.id | |
from invoices | |
join transactions | |
on invoices.id = transactions.invoice_id | |
where transactions.result = 'failed' | |
and invoices.merchant_id = #{self.id} | |
except | |
select invoices.id | |
from invoices | |
join transactions | |
on invoices.id = transactions.invoice_id | |
where transactions.result = 'success' | |
and invoices.merchant_id = #{self.id} | |
) | |
and invoices.merchant_id = #{self.id};") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment