This project is archived and is in readonly mode.

#848 ✓resolved
Nathan Witmer

finder_sql in has_and_belongs_to_many is only interpolated once

Reported by Nathan Witmer | August 16th, 2008 @ 08:44 PM | in 2.x

class Project < ActiveRecord::Base
  has_and_belongs_to_many :developers, :finder_sql => 'select d.* from developers d inner join developers_projects dp on = dp.developer_id where dp.project_id = #{id}'

class Developer < ActiveRecord::Base

Given two projects and a few developers, say, project 1 has a, b, c, and project 2 has d and e:

# interpolates the SQL for project id 1
Project.find(1).developers # => [a, b, c]
# but now it's been permanently altered:
Project.find(2).developers # => [a, b, c], should be [d, e]

The problem is in the habtm construct_sql method, which replaces the reflection's :finder_sql option with the interpolated version. Instead, this should be recalculated every time the habtm association is instantiated.

I have attached a test case and the patch to fix this bug.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>


Referenced by