From 46e3ff3307531b50f160c69fc8cd510964e5005f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20Junstr=C3=B6m?= Date: Tue, 29 Sep 2026 14:00:38 +0200 Subject: [PATCH] Build where_ scopes with Arel instead of ActiveRecord::Base.connection Quoting the column through `connection` permanently checks out a connection, which Rails 7.2 soft-deprecates and raises on under `permanent_connection_checkout = :disallowed`. Using arel_table and Arel's bitwise operators lets Active Record quote when the query runs. --- lib/active_flag.rb | 18 +++++++++--------- test/active_flag_test.rb | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/active_flag.rb b/lib/active_flag.rb index 8497e7b..cc540f0 100644 --- a/lib/active_flag.rb +++ b/lib/active_flag.rb @@ -47,30 +47,30 @@ def flag(column, keys) # Scopes define_singleton_method "where_#{column}" do |*args| - integer, column_name = send("_where_#{column}", *args) - where("#{column_name} & #{integer} > 0") + integer, attribute = send("_where_#{column}", *args) + where((attribute & integer).gt(0)) end define_singleton_method "where_all_#{column}" do |*args| - integer, column_name = send("_where_#{column}", *args) - where("#{column_name} & #{integer} = #{integer}") + integer, attribute = send("_where_#{column}", *args) + where((attribute & integer).eq(integer)) end define_singleton_method "where_not_#{column}" do |*args| - integer, column_name = send("_where_#{column}", *args) - where("#{column_name} & #{integer} = 0") + integer, attribute = send("_where_#{column}", *args) + where((attribute & integer).eq(0)) end define_singleton_method "where_not_all_#{column}" do |*args| - integer, column_name = send("_where_#{column}", *args) - where("#{column_name} & #{integer} < #{integer}") + integer, attribute = send("_where_#{column}", *args) + where((attribute & integer).lt(integer)) end # utility method to extract parameters define_singleton_method "_where_#{column}" do |*args| return [ active_flags[column].to_i(args), - "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column)}" + arel_table[column] ] end end diff --git a/test/active_flag_test.rb b/test/active_flag_test.rb index 3f31a30..af2a4c5 100644 --- a/test/active_flag_test.rb +++ b/test/active_flag_test.rb @@ -162,4 +162,20 @@ def test_joining_tables assert_equal User.joins(:profile).where(profile: Profile.where_languages(:japanese)).count, 1 assert_equal User.joins(:profile).where(profile: Profile.where_languages(:chinese)).count, 0 end + + def test_scope_without_permanent_connection_checkout + skip unless ActiveRecord.respond_to?(:permanent_connection_checkout) + + previous = ActiveRecord.permanent_connection_checkout + ActiveRecord::Base.release_connection + ActiveRecord.permanent_connection_checkout = :disallowed + + assert_equal 2, Profile.where_languages(:english).count + assert_equal 1, Profile.where_all_languages(:english, :japanese).count + assert_equal 1, Profile.where_not_languages(:english).count + assert_equal 2, Profile.where_not_all_languages(:english, :japanese).count + refute ActiveRecord::Base.connection_pool.active_connection? + ensure + ActiveRecord.permanent_connection_checkout = previous if previous + end end