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 support for multilevel many-to-many counters #367

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

Conversation

SlayerDF
Copy link

@SlayerDF SlayerDF commented Apr 1, 2023

No description provided.

Copy link
Owner

@magnusvk magnusvk left a comment

Choose a reason for hiding this comment

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

I think this looks good—is it worth adding a note to the Readme file about this?

if value.is_a?(ActiveRecord::Associations::CollectionProxy)
value.map { |v| v.try(relation_primary_key(original_relation, source: obj, was: was).try(:to_sym)) }
else
value.try(relation_primary_key(original_relation, source: obj, was: was).try(:to_sym))
Copy link
Owner

Choose a reason for hiding this comment

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

Can we pull this out into a helper method like

def relation_primary_key_from_model(model, original_relation, obj, was)
  model.try(relation_primary_key(original_relation, source: obj, was: was).try(:to_sym)
end

Not sure if that's worth it, but it makes it clearer that the same thing is happening in both cases, just mapping in one.

@@ -0,0 +1,4 @@
class Reader < ActiveRecord::Base
Copy link
Owner

Choose a reason for hiding this comment

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

I might be reading this wrong, but it looks like the Reader class isn't actually required for the test scenario here, is it?

Copy link
Author

Choose a reason for hiding this comment

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

Actually, the problem with this scenario cannot be solved so easily, and the implementation is too naive. In the scenario, we have a ReadersArticle that updates the readers_count counter. Everything works fine until we delete the associated AuthorsArticle, which leads to the counter becoming out of sync.

Therefore, I don't see much benefit in this functionality. Unless leaving the technical possibility of specifying a multi-level path for the counter, and letting the implementation be up to a user, as it should be quite complex.

Copy link
Owner

Choose a reason for hiding this comment

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

Should we close this then?

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.

None yet

2 participants