This project is archived and is in readonly mode.

#2990 ✓resolved
James Pearson

[PATCH] Non standard ID column on has_many :through

Reported by James Pearson | August 3rd, 2009 @ 10:27 PM

Usingthe example code below:

class User < ActiveRecord::Base
has_many :followers has_many :suspects, :through => :followers end

class Connection < ActiveRecord::Base
belongs_to :user belongs_to :suspect, :primary_key => :case_id , :foreign_key => :case_id end

class Suspect < ActiveRecord::Base
belongs_to :connection, :primary_key => :case_id , :foreign_key => :case_id end

The problem is that the belongs_to seems to ignore the :primary key.

If I do

u = User.find(:first)

The SQL generated is:

SELECT suspects.* FROM suspects INNER JOIN connections ON = connections.case_id WHERE ((followers.user_id = 1))

However it should be:

SELECT suspects.* FROM suspects INNER JOIN connections ON suspects.case_id = connections.case_id WHERE ((followers.user_id = 1))

This was partially fixed in 2.3.3 but not for has_many :through.

The patch applied to 2.3.3 can be found here:

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