ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Non-boolean values passed to boolean predicates are no longer cast

Open dennmart opened this issue 3 months ago • 12 comments

The changes made in #1559 changed how values are casted for some/all boolean predicates like not_null.

Before the changes from the PR, when passing a non-boolean value to a boolean predicate (for example: name_not_null = 0), the Ransack::Nodes::Condition#casted_values_for_attribute method would call Ransack::Nodes::Value#cast using the predicate or attribute type. When the predicate type is boolean, the value would be cast to a proper boolean.

The changes in the PR just pass the value directly without any casting, so when calling the arel_predicate proc for the predicate, it will evaluate to true because 0 is truthy. Using a boolean value (like name_not_null = false) works as expected.

I'm not sure if this is a bug and these values should be properly cast, or if we're expected with Ransack 4.4.0 to use boolean values for these types of predicates.

Original issue https://github.com/activerecord-hackery/ransack/issues/1555

dennmart avatar Sep 26 '25 07:09 dennmart

Same issue here. Version 4.4.0 breaks all my code. Is this a bug or an intentional breaking change?

pioz avatar Sep 26 '25 13:09 pioz

@matheussilvasantos what are your thoughts on this issue?

Thank you for the report @dennmart and @pioz - this does appear to be a bug. Please also see the request in this issue for a large number of changes coming to Ransack.

scarroll32 avatar Sep 28 '25 15:09 scarroll32

Same issue here. Version 4.4.0 breaks all my code. Is this a bug or an intentional breaking change?

Not intentional.

scarroll32 avatar Sep 28 '25 16:09 scarroll32

@deivid-rodriguez can help more than me in this case.

matheussilvasantos avatar Sep 28 '25 18:09 matheussilvasantos

Same issue for timestamp column.

app(dev)> Time.zone
=> #<ActiveSupport::TimeZone:0x00007fb8e1a708b8 @name="Tokyo", @tzinfo=#<TZInfo::DataTimezone: Asia/Tokyo>, @utc_offset=nil>

# 4.3.0
app(dev)> puts User.ransack(created_at_gteq: '2025-09-29T00:00').result.to_sql
SELECT "users".* FROM "users" WHERE "users"."created_at" >= '2025-09-28 15:00:00'

# 4.4.0
app(dev)> puts User.ransack(created_at_gteq: '2025-09-29T00:00').result.to_sql
SELECT "users".* FROM "users" WHERE "users"."created_at" >= '2025-09-29 00:00:00'

kzkn avatar Sep 29 '25 08:09 kzkn

Hmm I think we have to revert this PR until we can solve it. I will revert it and release v4.4.1 as a patch. See this note https://github.com/activerecord-hackery/ransack/issues/1640#issuecomment-3346334948

If anyone else wants to do a PR to fix it, or just with the failing tests, that would be great.

scarroll32 avatar Sep 29 '25 10:09 scarroll32

Reverting with PR https://github.com/activerecord-hackery/ransack/pull/1645 and releasing v4.4.1

scarroll32 avatar Sep 29 '25 11:09 scarroll32

Reverting with PR #1645 and releasing v4.4.1

All tests are green. Version 4.4.1 appears to resolve the issue.

pioz avatar Sep 29 '25 12:09 pioz

I can confirm that the latest release fixes my issue with the boolean predicates 👍

dennmart avatar Sep 29 '25 12:09 dennmart

Thanks for the report and quick revert. I'll look into this again, considering this regression 👍.

deivid-rodriguez avatar Sep 29 '25 13:09 deivid-rodriguez

@deivid-rodriguez I think it is pretty close, this looks like the culprit

I'm actually in between jobs at the moment, I can also take a look at it.

scarroll32 avatar Sep 29 '25 14:09 scarroll32

Sure, please go ahead! If when I get to this it's not yet fixed, I can take over the fix wherever you left it 👍.

deivid-rodriguez avatar Sep 29 '25 17:09 deivid-rodriguez