Fix eager loading of hierarchies by defining the primary key #465
+4
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When preloading an association via join (e.g. by using
eager_load, or by referencing the associated table in awherecondition), rails uses the primary key of the associated table in order to deduplicate the rows and build up the correct association. But the default generated hierarchies table from this gem doesn't have anidcolumn or anything else that rails can automatically detect as a primary key. This leads rails to incorrectly treat all the records as equal, and return only one at random.For example, calling
MyModel.eager_load(:ancestor_hierarchies).map(&:ancestor_hierarchies)currently returns incorrect data - only one hierarchy per model, no matter how deep the hierarchy actually is.This PR tells rails to use the combination of the three columns (which do have a unique index on them already in the generator!) as the primary key, and allows the example code to correctly return all the hierarchies for each model.
It also would presumably fix #294 though I haven't tested that.