ARTLoe
ARTLoe

Reputation: 1799

Writing a scope to display objects without associated objects - Rails 4

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

Answers (1)

VAD
VAD

Reputation: 2401

This should work

Event.includes(:payments).where(payments: { event_id: nil })

Upvotes: 1

Related Questions