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

Ticket #128 (closed defect: duplicate)

Opened 4 years ago

Last modified 4 years ago

collection object gets its own size and empty status wrong

Reported by: dblack@wobblini.net Assigned to: David
Priority: normal Milestone: 0.9
Component: ActiveRecord Version: 0.8
Severity: normal Keywords:
Cc:

Description

A collection object from a has_many property does not have correct Array-like behavior, in the #size/#length and #empty? methods.

Specifically, it reports its size as 0 and its empty status as true, when it actually contains 96 elements. The elements can be iterated or selected with #[].

Change History

10/25/04 22:18:17 changed by dblack@wobblini.net

See relevant code excerpts at:

http://www.rafb.net/paste/results/FLQv0b83.html

10/26/04 02:54:38 changed by bitsweat

  • version changed from 0.7 to 0.8.
  • milestone changed from 0.8 to 0.9.

This occurs when a has_many relation uses finder_sql. The association's #size method uses counter_sql which is generated from finder_sql with a case-sensitive gsub, only accepting "SELECT ... FROM ...", so "select ... from ..." and any other variations will fail. Some kind of sanity check on the passed finder_sql would probably be useful, if not just to catch typos and such.

Here is the simple fix:

--- orig_has_many_association.rb        Mon Oct 25 19:42:40 2004
+++ has_many_association.rb     Mon Oct 25 15:58:49 2004
@@ -6,7 +6,7 @@
         @conditions = options[:conditions]
         
         if options[:finder_sql]
-          @counter_sql = options[:finder_sql].gsub(/SELECT (.*) FROM/, "SELECT COUNT(*) FROM")
+          @counter_sql = options[:finder_sql].gsub(/SELECT (.*) FROM/i, "SELECT COUNT(*) FROM")
           @finder_sql = options[:finder_sql]
         else
           @counter_sql = "#{@association_class_primary_key_name} = '#{@owner.id}'#{@conditions ? " AND " + @conditions : ""}"

10/30/04 18:26:11 changed by bitsweat

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

Patched and resolved in Ticket #143.