Why Doesn’t Ransack Support Rails Enums Properly?
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
I believe this bug is in relation = @object.where(viz.accept(search.base)) on lib/ransack/adapters/active_record/context.rb.
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.
Maybe you can try https://github.com/shoma07/ransack-enum.
I think I found a fix and made @matheussilvasantos' spec pass. Feel free to give #1559 a try!
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