Commit 38f57d3b authored by Yorick Peterse's avatar Yorick Peterse

Add ApplicationRecord.where_exists

This is a simple method for generating a WHERE EXISTS clause, without
having to write it out manually. This pattern is used in several places,
so this method allows us to cut down the amount of repetition in the
future.
parent fcc225e7
......@@ -61,4 +61,8 @@ class ApplicationRecord < ActiveRecord::Base
def self.underscore
Gitlab::SafeRequestStore.fetch("model:#{self}:underscore") { self.to_s.underscore }
end
def self.where_exists(query)
where('EXISTS (?)', query.select(1))
end
end
......@@ -90,4 +90,12 @@ RSpec.describe ApplicationRecord do
expect(User.at_most(2).count).to eq(2)
end
end
describe '.where_exists' do
it 'produces a WHERE EXISTS query' do
user = create(:user)
expect(User.where_exists(User.limit(1))).to eq([user])
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment