Skip to content

Commit 8cc4716

Browse files
committed
Fix Type#cast to handle symbol values correctly in Rails 8.0 bulk operations
Rails 8.0 changed how Type#cast is used during insert_all/upsert_all operations, causing symbol enum values (e.g., :active) to fail. The previous implementation always delegated to @subtype.cast first, which could incorrectly transform symbol values before looking them up. This fix: - First attempts to find the enumerize value directly with the input value - Only delegates to @subtype.cast if the direct lookup fails - Ensures both symbol values (:active) and their database representations (1) work correctly This maintains backward compatibility while fixing bulk operations in Rails 8.0.
1 parent bd03b76 commit 8cc4716

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/enumerize/activerecord.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,14 @@ def serialize(value)
123123

124124
def cast(value)
125125
return value if @subtype.is_a?(Type)
126+
return value if value.is_a?(::Enumerize::Value)
126127

127-
if value.is_a?(::Enumerize::Value)
128-
value
129-
else
130-
@attr.find_value(@subtype.cast(value))
131-
end
128+
# First try to find the enumerize value directly
129+
enumerize_value = @attr.find_value(value)
130+
return enumerize_value if enumerize_value
131+
132+
# If not found, delegate to subtype then try to find value
133+
@attr.find_value(@subtype.cast(value))
132134
end
133135

134136
def as_json(options = nil)

0 commit comments

Comments
 (0)