Skip to content

Support Rails 8.1 in PolymorphicArrayValueExtension#68

Merged
raymasiclat merged 2 commits intomasterfrom
raymasiclat/rails-8-1-support
Apr 30, 2026
Merged

Support Rails 8.1 in PolymorphicArrayValueExtension#68
raymasiclat merged 2 commits intomasterfrom
raymasiclat/rails-8-1-support

Conversation

@raymasiclat
Copy link
Copy Markdown
Contributor

@raymasiclat raymasiclat commented Apr 30, 2026

Rails 8.1 changed ActiveRecord::PredicateBuilder::PolymorphicArrayValue#initialize from (associated_table, values) to (reflection, values), dropping the @associated_table ivar that this gem reads via @associated_table.send(:reflection) in type_to_ids_mapping. Without an update, every polymorphic-array predicate query (Model.where(thing: [a, b])) raises NoMethodError: undefined method 'reflection' for nil on Rails 8.1.

Fix

Read @reflection when set (Rails 8.1+), fall back to @associated_table.send(:reflection) for older Rails versions. The rest of type_to_ids_mapping runs unchanged.

 def type_to_ids_mapping
-  association = @associated_table.send(:reflection)
+  association = if instance_variable_defined?(:@reflection) && @reflection
+    @reflection
+  else
+    @associated_table.send(:reflection)
+  end

Also in this PR

  • Adds gemfiles/Gemfile.rails-8.1-stable to the CI matrix (with Ruby 3.0/3.1 exclusions — Rails 8.1 requires 3.2+)
  • Bumps version to 3.5.0
  • CHANGELOG entry

Verification

The existing spec at spec/polymorphic_integer_type_spec.rb:135 ("properly finds the object when passing an array of sources") exercises exactly this code path:

Link.where(source: [source])

Verified locally:

  • Rails 8.0: 41/41 passing (backwards compat preserved)
  • Rails 8.1: 41/41 passing (fix works)
  • Rails 8.1 without this fix: 1 failure — proves the existing test catches the regression and that this fix resolves it

Downstream impact

Several Clio repos currently need (or are about to need) in-app monkey patches for this on Rails 8.1 — clio/accounting (PR #15318), and likely clio/themis, clio/grow. Publishing 3.5.0 lets each of those bump the gem dep and prevent the need for in-app patches.

🤖 Generated with Claude Code

Rails 8.1 changed `ActiveRecord::PredicateBuilder::PolymorphicArrayValue#initialize`
from `(associated_table, values)` to `(reflection, values)`, dropping the
`@associated_table` ivar that this gem reads via
`@associated_table.send(:reflection)` in `type_to_ids_mapping`. Without an
update, every polymorphic-array predicate query (e.g. `Model.where(thing: [a, b])`)
raises `NoMethodError: undefined method 'reflection' for nil` on Rails 8.1.

The minimal fix: read `@reflection` directly when set (Rails 8.1+), fall back to
`@associated_table.send(:reflection)` for older Rails versions. The rest of the
method runs unchanged.

This also adds Gemfile.rails-8.1-stable to the CI matrix so future regressions
are caught upstream rather than in downstream apps.

## Verification

The existing spec at `polymorphic_integer_type_spec.rb:135` ("properly finds
the object when passing an array of sources") exercises exactly this code path:

  Link.where(source: [source])

Verified locally:
- Rails 8.0: 41/41 passing
- Rails 8.1: 41/41 passing
- Rails 8.1 *without* this fix: 1 failure (the array-of-sources test) —
  proves the test catches the regression and the fix resolves it.

Downstream apps (clio/accounting, clio/manage, clio/themis, etc.) currently
maintain in-app monkey patches for this; bumping to 3.5.0 lets them delete
those.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The existing array-of-sources test only exercises a single-type array
(`Link.where(source: [cat])`). The fix's value also covers multi-type
arrays (`Link.where(source: [cat, dog])`) — a broader branch of
`type_to_ids_mapping` where the result hash spans multiple keys.

Locks in coverage on both Rails 8.0 (backwards compat) and 8.1.

Suggested by code review.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@raymasiclat raymasiclat marked this pull request as ready for review April 30, 2026 19:44
@raymasiclat raymasiclat requested review from a team as code owners April 30, 2026 19:44
Copy link
Copy Markdown

@ChrisRoversClio ChrisRoversClio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

manual and automated review. CI green. tests look good. LGTM

@raymasiclat raymasiclat merged commit ebe8c81 into master Apr 30, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants