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

Ticket #3403 (closed defect: duplicate)

Opened 3 years ago

Last modified 1 year ago

finder_sql for HABTM associations is only interpolated once

Reported by: solo [at] gatelys [dot] com Assigned to: David
Priority: high Milestone: 1.1
Component: ActiveRecord Version: 1.0.0
Severity: major Keywords:
Cc:

Description

If a class defines a HABTM association using :finder_sql, the finder_sql statement is only interpolated once. Subsequent calls to the association execute the identical SQL statement.

For example, say you have classes like:

class Category < ActiveRecord::Base
  has_and_belongs_to_many :items, :finder_sql => '
SELECT i.*
  FROM items i INNER JOIN category_item_map m ON m.item_id = i.id
 WHERE m.category_id = #{id}'
end

class Item < ActiveRecord::Base
end

You retrieve a list of categories, and want to iterate over the items association for each category in your list. When the finder_sql is interpolated the first time, the "#{id}" will be replaced by the id of the current category model. Subsequent SQL calls will use the same statement--id will still be for the first category.

This applies to edge and stable.

Attachments

illustrate_finder_sql_bug.patch (4.9 kB) - added by solo [at] gatelys [dot] com on 01/06/06 01:25:42.
fixtures and tests illustrating the defect

Change History

01/06/06 01:25:42 changed by solo [at] gatelys [dot] com

  • attachment illustrate_finder_sql_bug.patch added.

fixtures and tests illustrating the defect

03/27/06 18:31:37 changed by anonymous

  • milestone set to 1.1.

03/27/06 21:39:49 changed by jrm02t [AT] gmail [DOT] com

I have the same problem b/c I have to use Oracle 8i and thus have to use finder_sql to find associated records.

I would imagine anyone on 8i or using finder_sql for legacy databases will have this problem.

I also blogged it here so if anyone google's they'll have some hope of making it here to see the status.

06/29/06 12:06:42 changed by rob@oxdi.eu

I have made an unverified patch for this here:

http://dev.rubyonrails.org/ticket/5548

Use at your own risk as I was unable to assess full implications, but the current ActiveRecord unit tests passed OK with it.

06/29/06 16:01:25 changed by bitsweat

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

06/27/07 08:49:59 changed by bitsweat