Skip to content

support has_many :through associations#296

Merged
flavorjones merged 3 commits intomasterfrom
flavorjones-239-has-many-through
Dec 6, 2023
Merged

support has_many :through associations#296
flavorjones merged 3 commits intomasterfrom
flavorjones-239-has-many-through

Conversation

@flavorjones
Copy link
Collaborator

@flavorjones flavorjones commented Dec 2, 2023

This PR closes #239.

The first commit removes unused (and broken?) code that I found while looking at the association code. It was part of e6a47bc and should not have been present.

The second commit refactors the extensions spec a bit to minimize the test classes being created for each test; this makes the tests more explicit and speeds them up slightly.

The final commit introduces the test from #239 and overrides the has_many method in any class that includes ActiveHash::Associations::ActiveRecordExtensions. The new implementation will adopt new behavior if the :through option is present and it points at an ActiveHash::Base class:

  • retrieving the join models (e.g., "Patient#appointments")
  • calling the :through association name on each join model (e.g., "Appointment#physician")
  • and returning the unique matching records

Otherwise has_many will just call super.

`belongs_to :through` is not a thing, and this code should not have
been included in e6a47bc
this will allow me to add new classes in the next commit, but should
also speed things up slightly.
@flavorjones flavorjones requested a review from kbrock December 2, 2023 16:54
@flavorjones flavorjones changed the title fix: has_many :through associations support has_many :through associations Dec 2, 2023
klass_name = association_id.to_s.classify
klass =
begin
klass_name.constantize
Copy link
Collaborator

Choose a reason for hiding this comment

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

This makes me nervous.
I believe has_many is executed within a lock since it is defining a class.

Think this issue explains the issue: rails/rails#34310

In our projects, when we are defining a class, we work hard to not define a second one.
Not sure if this race condition still exists.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok, this is the same code path as our belongs_to fix.

I wasn't able to follow the association builder code to find an answer
looks like they do call safe_constantize (sparingly)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm. I'll create a separate PR converting all our invocations to safe_constantize.

Copy link
Collaborator

Choose a reason for hiding this comment

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

lol. I was talking more about ActiveSupport::Dependencies.interlock.loading, which probably goes away once zeitwerk is introduced. But sounds good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤦 I definitely did not read carefully enough. Apologies for missing your point, I get it now.

Copy link
Collaborator

@kbrock kbrock left a comment

Choose a reason for hiding this comment

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

@flavorjones the 2 issues I see are not really introduced by this PR but just propagation of existing issues.

If I find out how rails fixes this, then I'll introduce a fix later on. (I'm not hopeful though)

Lets merge since this is a great fix.

@flavorjones flavorjones merged commit 523bfae into master Dec 6, 2023
@flavorjones flavorjones deleted the flavorjones-239-has-many-through branch December 6, 2023 16:11
kbrock added a commit to kbrock/active_hash that referenced this pull request Apr 29, 2024
- Support `has_many :through` associations active-hash#296 @flavorjones
- Rails 7.1 support active-hash#291 @y-yagi

- Rails 7.1: fix sqlite3 issue active-hash#303 @flavorjones
- Rails 7.1.3: add missing `has_query_constraints?` active-hash#300 @flavorjones
- `Array#pluck` supports methods active-hash#299 @iberianpig
- Prefer `safe_constantize` over `constantize` active-hash#297 @flavorjones
- Treat `nil` and `blank?` as different values active-hash#295 @kbrock
- Fix `#where` for string keys active-hash#292 @usernam3
kbrock added a commit to kbrock/active_hash that referenced this pull request Apr 29, 2024
- Ruby 3.3 support active-hash#298 @m-nakamura145
- Support `has_many :through` associations active-hash#296 @flavorjones
- Rails 7.1 support active-hash#291 @y-yagi

- Rails 7.1: fix sqlite3 issue active-hash#303 @flavorjones
- Rails 7.1.3: add missing `has_query_constraints?` active-hash#300 @flavorjones
- `Array#pluck` supports methods active-hash#299 @iberianpig
- Prefer `safe_constantize` over `constantize` active-hash#297 @flavorjones
- Treat `nil` and `blank?` as different values active-hash#295 @kbrock
- Fix `#where` for string keys active-hash#292 @usernam3
@kbrock kbrock mentioned this pull request Apr 29, 2024
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.

The has_many :through Association does not get record

2 participants