Skip to content

Conversation

@Tobias-Knudsen
Copy link

@Tobias-Knudsen Tobias-Knudsen commented Nov 11, 2025

I’ve noticed some inconsistent behavior when using the _in predicate in Ransack compared to ActiveRecord.

For example, in plain Rails:

Vehicle.where(brand: [nil])
# => SELECT "vehicles".* FROM "vehicles" WHERE "vehicles"."brand" IS NULL
Vehicle.where(brand: [nil, 'AUDI'])
# => SELECT "vehicles".* FROM "vehicles" WHERE ("vehicles"."brand" = $1 OR "vehicles"."brand" IS NULL)

We use Ransack in our API. Since Rails automatically strips any nil values from an array in params, it's not possible for us to use the standard _in predicate in our API. To accomplish this I've introduced a custom predicate _in_or_null as described here: #940 (comment)

This allows me to perform the second example by using brand_in_or_null: [ "AUDI" ], however if I would use brand_in_or_null: [], I would expect this to only give me the vehicles where the brand is null, however the current logic deletes the param and returns all records.

I've changed the logic in the Search class initializer, where it now allows an array with nil values.

Possibly related to: #524

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.

1 participant