I have two models: Sale and Payment
class Sale < ActiveRecord::Base
has_one :payment
end
class SaleCancelation < ActiveRecord::Base
belongs_to :payment
end
I want to create two scopes, "with payment" and "without payment".
"with_payment" works easyly:
class Sale < ActiveRecord::Base
scope :with_payment, joins( :payment )
end
But how can I create a scope that finds every sale that does not have an associated Payment?
How about:
scope :without_payment, where( 'id not in (select sales_id from payments)' )
Another way to do it:
scope :without_profile, lambda { includes(:user_profile).where('user_profiles.id is null') }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With