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

Ticket #8937 (closed defect: incomplete)

Opened 3 years ago

Last modified 3 years ago

[PATCH] HABTM associations do not allow :joins option

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

Description

While attempting to work around #8838, I found out that specifying :joins on a find scoped by a HABTM association is not allowed, because the finder uses an INNER JOIN to fetch the records. e.g.

# Fails due to duplicate table names
Tag.find(8).posts.find(:all, :include => :tags)

# Fails as :joins option is not included in the generated SQL
Tag.find(8).posts.find(:all, :joins => sql_to_manually_eager_load)

This patch lets you use :joins successfully in such situations. I'm not sure how to go about testing it, and for some reason opening base_test.rb is crashing my text editor (Notepad++). It's a one-liner and doesn't break any existing tests, though.

Attachments

habtm_with_joins_fix.diff (0.6 kB) - added by jcoglan on 07/10/07 13:13:51.
habtm_with_joins_fix_with_name_checking.diff (1.8 kB) - added by jcoglan on 07/10/07 15:12:51.

Change History

07/10/07 13:13:51 changed by jcoglan

  • attachment habtm_with_joins_fix.diff added.

07/10/07 15:12:51 changed by jcoglan

  • attachment habtm_with_joins_fix_with_name_checking.diff added.

07/10/07 15:15:33 changed by jcoglan

I'm adding another patch that solves the problem slightly more intelligently by checking for duplicate table names between sets of JOIN statements before including the :joins option in the generated SQL.

07/12/07 22:13:18 changed by lifofifo

  • status changed from new to closed.
  • resolution set to incomplete.

Please add verifying test cases and reopen the ticket.

Thanks.