Skip to content

Conversation

snaka
Copy link

@snaka snaka commented Aug 21, 2025

Important

To run tests on this PR, it is necessary to merge the following PRs first

Motivation / Background

This Pull Request has been created because Rails 8.0 introduced changes to how Type#cast is used during bulk operations (insert_all, upsert_all), causing enumerated attributes with symbol values to fail. The issue affects only bulk operations while regular attribute assignment continues to work correctly.

Fixes #462

Detail

This Pull Request changes the Type#cast method in enumerize to handle values more intelligently for Rails 8.0 compatibility.

Steps to reproduce

class User < ActiveRecord::Base
  enumerize :status, in: { active: 1, inactive: 0 }
end

# This works in Rails 7.x but fails in Rails 8.0
User.insert_all([{ status: :active }])
User.upsert_all([{ status: :active }])

Expected behavior

Bulk operations (insert_all, upsert_all) should work correctly with symbol enum values like :active, just like regular attribute assignment does.

Actual behavior

In Rails 8.0, bulk operations fail when using symbol enum values because the Type#cast method incorrectly delegates to @subtype.cast first, which returns nil for symbols when the subtype is an integer type.

System configuration

Rails version: 8.0

Ruby version: 3.1.6

Testing

Added comprehensive tests for:

  • Type#cast behavior with symbols, strings, and integers
  • insert_all with mixed value types
  • upsert_all with different value formats
  • Handling of invalid enum values
  • Preservation of existing Enumerize::Value objects

All tests pass on Rails 7.0, 7.1, and 8.0.

snaka added 2 commits August 21, 2025 21:45
- Test Type#cast returns Enumerize::Value for symbols, integers, and
  strings
- Test Type#cast handles invalid values and preserves existing Value
  objects
- Test insert_all/upsert_all work with mixed value types and handle
  invalid values
…rations

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tests for upsert_all and insert_all fail in Rails v8
1 participant