Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

finder_sql in has_and_belongs_to_many is only interpolated once

#848

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

class Developer < ActiveRecord::Base
end

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.

Reported by Nathan Witmer · August 16th, 2008 @ 08:44 PM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Nathan Witmer
    Nathan Witmer

    I should note that removing interpolate_sql_options! doesn't affect anything else, it was only being used in the habtm association.

    August 16th, 2008 @ 08:49 PM

  2. Repository
    Repository
    • State changed from new to resolved

    (from [8cfdcdb35db6e2f6fd5a72a38f4352beab148af1]) Updated has_and_belongs_to_many association to fix :finder_sql interpolation. [#848 finder_sql in has_and_belongs_to_many is only interpolated once state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

    August 16th, 2008 @ 10:25 PM

  3. Repository
    Repository

    (from [4c071bc796e07e33497937bdc92d3d8d3ef365f1]) Updated has_and_belongs_to_many association to fix :finder_sql interpolation. [#848 finder_sql in has_and_belongs_to_many is only interpolated once state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

    August 25th, 2008 @ 09:30 AM