I have a Payment Model which is polymorphic on activities
class Payment
belongs_to :activity, polymorphic: true
end
one of the activities is referrals.
class Referral
has_many :payments, :as => :activity
def self.unpaid
where(payments.count == 0)
end
end
I want to create a class method so I can get all of the referrals which are unpaid. I was using this approach above, but getting a undefined_method error on payments.
Referral.first.payments.count works fine.
what am I missing here?
It sounds to me what you’re looking for are ActiveRecord Scopes. Using a scope, you’d do something like this:
This will perform a join on payments, and then the condition will find all columns that have no payments.id (in other words, with no payments). Then you could do Referral.unpaid.all, and see all referrals where there are no joined payments.