Open
Conversation
pekopekopekopayo
commented
Feb 4, 2026
|
|
||
| def cast(type) | ||
| case type | ||
| when :enum |
Contributor
Author
There was a problem hiding this comment.
While enum is not technically a database type, it is treated as one here exceptionally to prevent Ransack from incorrectly casting the value as an integer.
Contributor
Author
|
Could you please review this? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1555
Related: #1644
Problem
When searching enum attributes by key (e.g.,
temperament_eq: 'choleric'), Ransack identified the column type as:integerand cast the value viaString#to_i, resulting in'choleric'.to_i == 0. This produced incorrect queries likeWHERE temperament = 0instead ofWHERE temperament = 2.Solution
In Rails,
Person.where(temperament: 'choleric')andPerson.where(temperament: 2)both produce the same query — ActiveRecord handles the mapping internally. Ransack should behave the same way. To achieve this,Attribute#typenow returns:enumfor enum attributes, andValue#cast(:enum)passes the raw value through to ActiveRecord instead of attempting its own conversion.A regression test for #1644 has also been included to ensure boolean predicate casting remains unaffected.
Changes
Attribute#type— returns:enumwhen the attribute is a defined enumValue#cast(:enum)— returns raw value, delegating conversion to ActiveRecord