This project is archived and is in readonly mode.
[PATCH] ActiveRecord count fails when run with multiple includes and a join
-
Ernie Miller
Good catch. As the guy who did the original refactor in associations.rb to add the graft method, I wanted to weigh in.
def find_parent_in(other_join_dependency)
other_join_dependency.joins.detect do |join| self.parent == join endend
The other JoinBase is other_join_dependency.joins.first, so if it were equal it would be getting used. I'm assuming that this is happening because they're technically not "equal" -- that is, one JoinBase has preexisting values in its table_joins attribute. I'm not saying we should change "==" on JoinBase because it's technically correct to consider all attributes in testing equality, but just giving a bit more background as to what's going on.
It seems like your fallback is as good as any, as if the association doesn't exist on the JoinBase it'll raise an error during build.
So I guess that's a long way to saying: +1. :p
-
David Genord II
Ken
These errors do not appear to be linked. To trigger this error you must be using both includes and joins, running a count operation, and it only effects association lookup. -
Repository
- State changed from new to resolved
(from [1fcf4e8ecbe4db2443a88d1e06c06667cff5b762]) JoinDependency#graft does not properly set parent join [#5124 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/1fcf4e8ecbe4db2443a88d1e06c066... -
Anatoliy Lysenko
I think the original test isn't good enough, so your fix doesn't work for me.
When you use plain include, when each included association is associated with base model it works:
Author.joins(:special_posts).includes([:posts, :categorizations])
When find_parents_in(self) can't find parent for posts we just join it to Author - pass.But fail when you use cascade includes.
Category.joins(:categorizations).includes([{:posts=>:comments}, :authors])
When find_parents_in(self) can't find parent for :posts we join it to Category - pass.
When find_parents_in(self) can't find parent for :comments we join to Category - fail. "ActiveRecord::ConfigurationError: Association named 'comments' was not found; perhaps you misspelled it?". Because Category haven't comments association.I can't find the reason, so I just change JoinBase#== method as Ernie suggested.
Patch is attached. But it fix only symptom... not sure...
-
Anatoliy Lysenko
I dislike my patch and create separate ticket #5845
