Skip to content

Handle belongs_to :through, :polymorphic: true#336

Open
kbrock wants to merge 3 commits intoactive-hash:masterfrom
kbrock:issue_334
Open

Handle belongs_to :through, :polymorphic: true#336
kbrock wants to merge 3 commits intoactive-hash:masterfrom
kbrock:issue_334

Conversation

@kbrock
Copy link
Copy Markdown
Collaborator

@kbrock kbrock commented Aug 18, 2025

Thank you, @alexgriff, for the outline.

  • Fix the through and polymorphic nature.
  • No longer access the schema at class load time.
  • Also fixed the belongs_to in another spot that had the same issue.
  • NOTE: belongs_to_active_hash is no longer called.

There is a lock on loading the schema. There is a lock to load a class. When you access the schema at class load time, but then access another class that needs to be loaded, you get a deadlock.

So changed the code to avoid.

Fixes #334

We undefine these classes in the after {}
So in theory, they should not be a duplicate.

rspec often uses stub_const, but active record has issues with this since
they dig into the class.name and stuff
@kbrock kbrock force-pushed the issue_334 branch 2 times, most recently from f91dc58 to b3a12f3 Compare April 4, 2026 06:07
kbrock and others added 2 commits April 6, 2026 22:20
Move class resolution out of definition time and into method bodies.
Call super first so AR registers reflections (preserving eager loading
support), then override accessors with runtime ActiveHash checks.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When source_type is present, resolve the class at runtime and look up
by foreign key directly, bypassing AR's polymorphic belongs_to which
does not work with ActiveHash. Fixes active-hash#334.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kbrock
Copy link
Copy Markdown
Collaborator Author

kbrock commented Apr 7, 2026

update: removed class load
update: removed another class loader (total tangent, but solves a similar issue)

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.

Polymorphic has_many with a source_type raise an exception in ActiveHash 4.0

1 participant