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.

has_many :through with custom primary keys

#736

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.

Reported by Justin Marney · July 31st, 2008 @ 09:43 PM

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

Activity

  1. Justin Marney
  2. Justin Marney
    Justin Marney

    IGNORE THE ABOVE LINK

    Sorry pasted the wrong link. Go HERE http://www.ruby-forum.com/topic/... for more discussion.

    July 31st, 2008 @ 09:54 PM

  3. Peter Liu
    Peter Liu

    I 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.

    October 2nd, 2008 @ 08:33 AM

  4. Tim Pope
    Tim Pope

    I 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.

    October 21st, 2008 @ 10:36 PM

  5. Will Bryant
    Will Bryant

    That 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)?

    January 9th, 2009 @ 10:27 AM

  6. Repository
    Repository
    • 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...

    March 9th, 2009 @ 01:45 PM