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

Ticket #8947 (new defect)

Opened 10 months ago

Last modified 8 months ago

[PATCH] HABTM scoped finds fail when using :limit and :include with duplicate links

Reported by: jcoglan Assigned to: core
Priority: normal Milestone: 1.2.7
Component: ActiveRecord Version: edge
Severity: normal Keywords: unverified
Cc:

Description

Calling find on a HABTM-scoped collection returns incorrect results under some circumstances if there are duplicate links in the database. I've set :uniq => true on the relationship between Post and Category and written some test cases to demonstrate. In the patch, the second and fifth tests are the ones that fail, i.e.

# Returns duplicate results
category.posts.find(:all)

# Returns too few results as uniqueness is imposed in Ruby after the results
# come back from the DB, rather than being enforced by the DB query
category.posts.find(:all, :limit => 2, :include => :author_with_posts)

I've been trying to work around this in my project and I'm going round in circles trying to get both eager loading and result set size to work correctly. I'm not sure that the first example is really a bug, as the find is pretty much redundant and could be useful if you actually want to pull out duplicates from the DB. The second example is infuriating though.

Attachments

habtm_duplicate_links_tests.diff (3.5 kB) - added by jcoglan on 07/11/07 11:29:51.

Change History

07/11/07 11:28:44 changed by jcoglan

Just realised the bug will not appear if you eager load at least one has_many / habtm association, because of the way that using_limitable_reflections? works. I tried removing the check for using_limitable_reflections? from construct_finder_sql_with_included_associations but it breaks some tests. Anyhow, I'm adding an extra test to the patch to confirm which includes fire the bug.

07/11/07 11:29:51 changed by jcoglan

  • attachment habtm_duplicate_links_tests.diff added.

09/16/07 10:42:28 changed by jcoglan

  • keywords set to unverified.