Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8267 (new defect)

Opened 1 year ago

Last modified 1 year ago

[PATCH] Cascaded eager loading fails to create unique table aliases with habtm/has_many through joins

Reported by: mattwestcott Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc: mattwestcott

Description

In the situation where a table appears twice in a finder query:

  • firstly as an intermediate join in a habtm / has_many :through association, and
  • secondly, appearing in a cascaded eager loading clause anywhere deeper than the top level;

the table does not receive a unique table alias when the eager loading clause is constructed. For example:

class Author < ActiveRecord::Base
  has_many :comments_with_cited_posts, :through => :posts
end

class Post < ActiveRecord::Base
  has_many :comments_with_cited_posts, :class_name => "Comment",
       :include => {:post_citations => :post}
end

In the Author#comments_with_cited_posts query, the 'posts' table is joined twice but does not get aliased as part of the eager loading.

As far as I can tell, this is due to the following snippet in associations.rb:

      if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{aliased_table_name.downcase}\son}
        join_dependency.table_aliases[aliased_table_name] += 1
      end

which is supposed to scan the SQL for an existing join on 'posts', but at that point parent.table_joins is blank, presumably because 'parent' refers to the :post_citations association rather than the query as a whole.

Test case attached.

Attachments

test_multiple_table_references_on_cascaded_eager_loading.diff (2.9 kB) - added by mattwestcott on 05/04/07 16:49:05.
currently-failing test case
multiple_table_references_on_cascaded_eager_loading_fix.diff (3.7 kB) - added by mattwestcott on 05/10/07 23:42:23.

Change History

05/04/07 16:49:05 changed by mattwestcott

  • attachment test_multiple_table_references_on_cascaded_eager_loading.diff added.

currently-failing test case

05/04/07 16:52:00 changed by mattwestcott

Just to add - despite appearances, this doesn't seem to be the same issue as ticket:7503 (where both of the offending joins are part of the eager loading clause).

05/10/07 23:42:23 changed by mattwestcott

  • attachment multiple_table_references_on_cascaded_eager_loading_fix.diff added.

05/10/07 23:48:07 changed by mattwestcott

  • summary changed from Cascaded eager loading fails to create unique table aliases with habtm/has_many through joins to [PATCH] Cascaded eager loading fails to create unique table aliases with habtm/has_many through joins.

One-line fix attached... seems to do the trick.

(Following code paths in associations.rb, it appears that the JoinBase is the only join in the dependency which ever gets table_joins set, so it's enough to just substitute parent.table_joins with join_dependency.join_base.table_joins.)