Reputation: 1799
i am really struggling with SQL search queries today. Can one kindly tell me how i write a scope that display events with no payments.
event.rb
has_many :payments
payment.rb
belongs_to :event
i tried writing the below scope in the terminal:
events.joins(:payments).where("event.payments.empty?")
i also tried:
events.where("payments.empty?")
i am very unsure how to write a scope that displays an object with an empty array
Upvotes: 1
Views: 35
Reputation: 2401
This should work
Event.includes(:payments).where(payments: { event_id: nil })
Upvotes: 1