Topic: [SOLVED] Scope users by "late" if invoice not paid

So I have 3 models:

#User.rb
  has_many :contracts
  has_many :invoices,  :through => :contracts

#Invoice.rb
  belongs_to :contract
  has_one :user,  :through => :contract

#Contract.rb
  belongs_to :user
  has_many :invoices

The "Contract" is simply a way of linking users and invoices together.

My question is, I'm wanting to find users that have past-due invoices. So, if invoice_total is greater than zero, and the invoice_due_date is past, I want to find the users that are related to that invoice.

#User.rb
  scope :late_payment, where()

How would I do this efficiently?

Thanks!

Last edited by wesf90 (2012-04-30 09:16:31)

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: [SOLVED] Scope users by "late" if invoice not paid

Something like this:

class User < AR
   scope :late_payment, joins(:invoices).where('invoice_total > ?' and 'invoice_due_date < ?', 0, Time.now)
end

May be you should adapt the comparison of date, it depends on what type of date you use, Time or Date. In this case just replace 'Time.now' block with what you need (e.g. Time.now.to_date, etc. to get a Date object)

Last edited by Javix (2012-04-30 07:08:57)

Re: [SOLVED] Scope users by "late" if invoice not paid

Great, thanks for the tip! smile

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!