ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Why Doesn’t Ransack Support Rails Enums Properly?

Open pekopekopekopayo opened this issue 9 months ago • 5 comments

Is this the intended behavior?

class Lesson < ApplicationRecord
  enum :status, {
    one: 1,
    two: 2,
    three: 3
  }
  class << self
    def ransackable_attributes(auth_object = nil)
      ["status"]
    end
  end
end
Lesson.ransack({ "status_eq" => "two" }).result 
# I expected: SELECT * FROM lessons WHERE status = 2

But the actual query is:

SELECT "lessons".* FROM "lessons" WHERE "lessons"."status" = 0

I understand that defining a ransacker in the model can be used as a workaround.
That said, if this is considered a bug, I would truly appreciate any fix or guidance.
If there's any way I could contribute to resolving it, I’d be more than happy to help.

I’m not very fluent in English, so I apologize if my words sounded rude

pekopekopekopayo avatar Apr 01 '25 15:04 pekopekopekopayo

I believe this bug is in relation = @object.where(viz.accept(search.base)) on lib/ransack/adapters/active_record/context.rb.

matheussilvasantos avatar May 19 '25 23:05 matheussilvasantos

I've created this spec to illustrate the bug: https://github.com/activerecord-hackery/ransack/pull/1559. I don't know how to fix it yet.

matheussilvasantos avatar May 19 '25 23:05 matheussilvasantos

Maybe you can try https://github.com/shoma07/ransack-enum.

matheussilvasantos avatar May 20 '25 13:05 matheussilvasantos

I think I found a fix and made @matheussilvasantos' spec pass. Feel free to give #1559 a try!

deivid-rodriguez avatar May 23 '25 15:05 deivid-rodriguez

Reopening due to https://github.com/activerecord-hackery/ransack/issues/1644 and https://github.com/activerecord-hackery/ransack/pull/1645

Adding to v5.0.0 planning

scarroll32 avatar Sep 29 '25 11:09 scarroll32