Skip to content

support enum#1665

Open
pekopekopekopayo wants to merge 1 commit intoactiverecord-hackery:mainfrom
pekopekopekopayo:feature/enum-support
Open

support enum#1665
pekopekopekopayo wants to merge 1 commit intoactiverecord-hackery:mainfrom
pekopekopekopayo:feature/enum-support

Conversation

@pekopekopekopayo
Copy link
Contributor

Fixes #1555
Related: #1644

Problem

When searching enum attributes by key (e.g., temperament_eq: 'choleric'), Ransack identified the column type as :integer and cast the value via String#to_i, resulting in 'choleric'.to_i == 0. This produced incorrect queries like WHERE temperament = 0 instead of WHERE temperament = 2.

Solution

In Rails, Person.where(temperament: 'choleric') and Person.where(temperament: 2) both produce the same query — ActiveRecord handles the mapping internally. Ransack should behave the same way. To achieve this, Attribute#type now returns :enum for enum attributes, and Value#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


def cast(type)
case type
when :enum
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@pekopekopekopayo
Copy link
Contributor Author

@deivid-rodriguez
@scarroll32

Could you please review this?

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.

Why Doesn’t Ransack Support Rails Enums Properly?

1 participant