From 5961aa90f068acf79607da21c736c8b011b7f287 Mon Sep 17 00:00:00 2001 From: Dirkjan Bussink Date: Sun, 27 Sep 2009 19:47:27 +0200 Subject: [PATCH] Ensure the correct ordering of links for more complex joins Consider a query like the following: Person.all(Person.memberships.group.type => 'cool', Person.memberships.valid_from.gte => Date.today) This can result in one of the following to link lists being set up (depending on non-deterministic hash ordering). [:memberships, :group, :memberships] [:group, :memberships, :memberships] Query#normalize_links walks through this list and creates the following structure: [:memberships, :group] or [:group, :memberships] If the first situation happens and the subsequent join statement is created, it will create an invalid join statement. DataObjectsAdapter#join_statement walks through the normalized list in revere order, so if that adds group to the query before memberships in a join, the query will fail. The fix is very simple. Because of how the initial list is constructed, a correctly normalized list will be created by walking the initial 3 element list given above in the reverse order. That will always result in the [:group, :memberships] links that will work correctly if joined in reverse order --- lib/dm-core/query.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/dm-core/query.rb b/lib/dm-core/query.rb index df9da306..fb6fd17e 100644 --- a/lib/dm-core/query.rb +++ b/lib/dm-core/query.rb @@ -925,7 +925,7 @@ def normalize_links @links.clear - while link = links.shift + while link = links.pop relationship = case link when Symbol, String then @relationships[link] when Associations::Relationship then link @@ -961,6 +961,7 @@ def normalize_links @links << relationship end end + @links.reverse! end # Append conditions to this Query