summaryrefslogtreecommitdiff
path: root/config/initializers/active_record_build_select.rb
blob: d072f272e3c031025ae4b4c18f4a7bf50254f3a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# rubocop:disable Gitlab/ModuleWithInstanceVariables

# build_select only selects the required fields if the model has ignored_columns.
# This is incompatible with some migrations or background migration specs because
# rails keeps a statement cache in memory. So if a model with ignored_columns in a
# migration is used, the query with select table.col1, table.col2 is stored in the
# statement cache. If a different migration is then run and one of these columns is
# removed in the meantime, the query is invalid.

module ActiveRecord
  module QueryMethods
    private

    def build_select(arel)
      if select_values.any?
        arel.project(*arel_columns(select_values.uniq))
      else
        arel.project(@klass.arel_table[Arel.star])
      end
    end
  end
end