Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add habtm/has_many :through support #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

antoinematyja
Copy link

Hello 🙂

According to https://www.rubydoc.info/gems/activerecord/ActiveRecord/Reflection/AbstractReflection
"Countable" associations are :

  • has_many
  • has_and_belongs_to_many
  • has_many :through

The current AssociationCountLoader works for has_many associations, but not for the other 2.

I added an implementation for habtm/has_many through which relies on joins with the association name (which should automatically go through all intermediate associations) + where with association name: primary key => array of primary keys (which should automatically create the where clause).

It also relies on an inverse_of definition, inspired by activerecord-precounter gem. I also added two errors (also inspired from the gem) in case of missing inverse_of, or in case of unhandled association.

Note: the activerecord-precounter gem also handles association scopes, which could be added to this loader.

@jamesbrooks
Copy link
Owner

Thanks for this @antoinematyja, I'll give it a test shortly but looks great - great for AssociationCountLoader to be able to handle as many relationship possibilities as it can :)

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.

2 participants