Commit 949f0610 authored by Yorick Peterse's avatar Yorick Peterse

Merge branch 'application-record-where-exists' into 'master'

Add ApplicationRecord.where_exists

See merge request gitlab-org/gitlab!45041
parents ea85a9a7 38f57d3b
...@@ -61,4 +61,8 @@ class ApplicationRecord < ActiveRecord::Base ...@@ -61,4 +61,8 @@ class ApplicationRecord < ActiveRecord::Base
def self.underscore def self.underscore
Gitlab::SafeRequestStore.fetch("model:#{self}:underscore") { self.to_s.underscore } Gitlab::SafeRequestStore.fetch("model:#{self}:underscore") { self.to_s.underscore }
end end
def self.where_exists(query)
where('EXISTS (?)', query.select(1))
end
end end
...@@ -90,4 +90,12 @@ RSpec.describe ApplicationRecord do ...@@ -90,4 +90,12 @@ RSpec.describe ApplicationRecord do
expect(User.at_most(2).count).to eq(2) expect(User.at_most(2).count).to eq(2)
end end
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 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