Skip to content

support enum#1665

Merged
gregmolnar merged 2 commits intoactiverecord-hackery:mainfrom
pekopekopekopayo:feature/enum-support
Mar 4, 2026
Merged

support enum#1665
gregmolnar merged 2 commits intoactiverecord-hackery:mainfrom
pekopekopekopayo:feature/enum-support

Conversation

@pekopekopekopayo
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

@deivid-rodriguez
@scarroll32

Could you please review this?

@gregmolnar gregmolnar enabled auto-merge (squash) March 4, 2026 15:25
@gregmolnar gregmolnar disabled auto-merge March 4, 2026 15:25
@gregmolnar gregmolnar merged commit e82f6ba into activerecord-hackery:main Mar 4, 2026
25 checks passed
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?

2 participants