diff options
Diffstat (limited to 'config/initializers/active_record_build_select.rb')
-rw-r--r-- | config/initializers/active_record_build_select.rb | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/config/initializers/active_record_build_select.rb b/config/initializers/active_record_build_select.rb new file mode 100644 index 00000000000..d072f272e3c --- /dev/null +++ b/config/initializers/active_record_build_select.rb @@ -0,0 +1,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 |