rails icon indicating copy to clipboard operation
rails copied to clipboard

Handle empty array value in query builder's tuple syntax

Open Slotos opened this issue 4 months ago • 3 comments

Specifically, .where([:id] => []) is now equivalent to where(:id => []).

Motivation / Background

This behaviour fixes #collection_singular_ids= for relations with composite query constraints when used in forms, as ids_writer compacts blank values before looking up matching records, efectively calling .where(primary_key => []) when, for example, all checkboxes were deselected by a user on a form.

The error indicative of the behaviour that this commit attempts to fix: Arel::Visitors::UnsupportedVisitError: Unsupported argument type: NilClass. Construct an Arel node instead.

Detail

#reduce returns nil when operating on empty collections. This commit adds a separate branch for handling an empty query set in PredicateBuilder#grouping_queries.

Checklist

Before submitting the PR make sure the following are checked:

  • [x] This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • [x] Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • [x] Tests are added or updated if you fix a bug or add a feature.
  • [x] CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Slotos avatar Feb 17 '24 15:02 Slotos