Ignore eager loading in Project.search UNION

The queries that are UNION'd together don't need any eager loading
(since we really only use the resulting SQL instead of having
ActiveRecord actually run the queries). By dropping any eager loaded
associations queries such as the following work instead of producing a
SQL error:

    Project.all.includes(:namespace).search('foo')
This commit is contained in:
Yorick Peterse 2016-03-15 16:46:17 +01:00
parent ea7d062fa6
commit c742760289
2 changed files with 13 additions and 0 deletions

View File

@ -286,7 +286,14 @@ class Project < ActiveRecord::Base
or(ptable[:description].matches(pattern))
)
# We explicitly remove any eager loading clauses as they're:
#
# 1. Not needed by this query
# 2. Combined with .joins(:namespace) lead to all columns from the
# projects & namespaces tables being selected, leading to a SQL error
# due to the columns of all UNION'd queries no longer being the same.
namespaces = select(:id).
except(:includes).
joins(:namespace).
where(ntable[:name].matches(pattern))

View File

@ -634,6 +634,12 @@ describe Project, models: true do
it 'returns projects with a matching namespace name regardless of the casing' do
expect(described_class.search(project.namespace.name.upcase)).to eq([project])
end
it 'returns projects when eager loading namespaces' do
relation = described_class.all.includes(:namespace)
expect(relation.search(project.namespace.name)).to eq([project])
end
end
describe '#rename_repo' do