Skip to content

Instantly share code, notes, and snippets.

@tmikeschu
Created February 7, 2017 12:51
Show Gist options
  • Save tmikeschu/e31f8db0567e63535982548a6c571d78 to your computer and use it in GitHub Desktop.
Save tmikeschu/e31f8db0567e63535982548a6c571d78 to your computer and use it in GitHub Desktop.
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