This project is archived and is in readonly mode.

has_many :through with custom primary keys
Reported by Justin Marney | July 31st, 2008 @ 09:43 PM | in 2.x
Given the following model:
Owner -has_many-> Bikes -has_many-> Spokes
class Owner
has_many :bikes
has_many :spokes, :through => :bikes
end
hmt will create an invalid sql statement for owner.spokes if set_primary_key is used. The source_primary_key was not pointing to the right primary key, but since all pks are 'id' in a normal rails model this problem was only seen when set_primary_key was used. This patch resolves the issue by changing the source_primary_key from @reflection.klass.primary_key to @reflection.through_reflection.klass.primary_key.
Comments and changes to this ticket
- 
            
         Justin Marney July 31st, 2008 @ 09:51 PMfor more discussion see: http://www.ruby-forum.com/topic/... 
- 
            
         Justin Marney July 31st, 2008 @ 09:54 PMIGNORE THE ABOVE LINK Sorry pasted the wrong link. Go HERE http://www.ruby-forum.com/topic/... for more discussion. 
- 
            
         Peter Liu October 2nd, 2008 @ 08:33 AMI have the same problem and have been pulling my hair out for days. It is a bug and your patch fixed it. I'm still new so I don't know how to apply the patch so I just manually changed that one line and it work. Please include this patch in 2.2. 
- 
            
         Tim Pope October 21st, 2008 @ 10:36 PMI should have searched for tickets first as I've solved this independently. I'm attaching a patch because the original no longer applies cleanly. Both patches implement the same solution with tests and get a +1 from me. 
- 
            
         Will Bryant January 9th, 2009 @ 10:27 AMThat looks better, but I think it may be still missing another possible case - if it goes through an association with a :primary_key option (eg. has_many :bikes, :primary_key => 'my_special_id'; has_many :spokes, :through => :bikes)? 
- 
         Repository March 9th, 2009 @ 01:45 PM- State changed from new to resolved
 (from [1e6c50e21bdb8c99116a7dc6921ef3eb4ed9531a]) Ensure has_many :through works with changed primary keys [#736 state:resolved] Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co... 
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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 
         736 
          has_many :through with custom primary keys
        (from [1e6c50e21bdb8c99116a7dc6921ef3eb4ed9531a]) Ensure
... 736 
          has_many :through with custom primary keys
        (from [1e6c50e21bdb8c99116a7dc6921ef3eb4ed9531a]) Ensure
...
 Justin Marney
      Justin Marney