I have the following schema in my rails app.
class Campaign < ApplicationRecord
has_many :businesses
has_many :clients, through: :businesses
end
class Business < ApplicationRecord
has_many :clients
belongs_to :campaign
end
class Client < ApplicationRecord
has_many :events
belongs_to :business
end
class Event < ApplicationRecord
belongs_to :client
enum category: {
:processed,
:delivered,
:opened
}
end
I need a way of finding all clients in a campaign that have a processed and delivered event category but not a opened category
My naive way of doing this was:
c = Campaign.first
c.clients.joins(:events).where('events.category' => [0, 1]).where.not('events.category' => [2])
But this won't work.
This query will run across some pretty big tables so eager loading isn't an option.