From 51640c4674bb7d7ee026096034798964c500c15d Mon Sep 17 00:00:00 2001 From: KJ Tsanaktsidis Date: Wed, 5 Apr 2023 15:14:02 +1000 Subject: [PATCH] Cache the is_sharded? status of the model Sets self.sharded depending on the superclass, if it is not otherwise set. --- lib/active_record_shards/model.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/active_record_shards/model.rb b/lib/active_record_shards/model.rb index 7d3c6581..97b43f5f 100644 --- a/lib/active_record_shards/model.rb +++ b/lib/active_record_shards/model.rb @@ -11,7 +11,11 @@ def not_sharded end def is_sharded? # rubocop:disable Naming/PredicateName - if self == ActiveRecord::Base + # "sharded" here means self.sharded, but actually writing "self.sharded" + # doesn't work until Ruby 2.7 (and this gem currently supports 2.6). + return sharded unless sharded.nil? + + self.sharded = if self == ActiveRecord::Base sharded != false && supports_sharding? elsif self == base_class if sharded.nil?