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.

preloading has_one :through on a belongs_to

#1507

Preloading a has_one :through on a belongs_to finds the records but doesn't correctly set the association.

An example (abbreviated from the tests):


class Member < ActiveRecord::Base
  belongs_to :member_type
end

class MemberDetail < ActiveRecord::Base
  belongs_to :member
  has_one :member_type, :through => :member
end

class MemberType < ActiveRecord::Base
  has_many :members # not relevant here
end

This case is supported in the regular association code, but doing


MemberDetail.find(:all, :include => :member_type)

leaves all the result records with member_type nil, but loaded_member_type? true.

The attached patch fixes this, and includes a test for this case.

It also includes a (tiny) fix for AssociationCollection#to_ary, where has_one :through associations would give an error (trying to call to_ary on a lone AR object).

Reported by Matt Jones · December 2nd, 2008 @ 09:40 PM

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. David Stevenson
    David Stevenson
    1. toary has Bren driving me crazy! Can't include past a has one thru cause of it!

    December 6th, 2008 @ 02:47 AM

  2. Pratik
    Pratik
    • Assigned user set to Pratik

    December 7th, 2008 @ 04:00 AM

  3. Michael Koziarski
  4. Pratik
    Pratik
    • Assigned user changed from Pratik to Frederick Cheung

    Fred : Looks good ?

    December 10th, 2008 @ 07:17 PM

  5. Frederick Cheung
    Frederick Cheung
    • Assigned user changed from Frederick Cheung to Pratik

    Looks sane to me

    December 10th, 2008 @ 09:57 PM

  6. Repository
    Repository
    • State changed from new to resolved

    (from [a9422cc1db9501a80ecf2c25a5d3b0c4f4f32763]) Fix preloading of has_one :through associations on belongs_to [#1507 preloading has_one :through on a belongs_to state:resolved]

    Signed-off-by: Frederick Cheung frederick.cheung@gmail.com http://github.com/rails/rails/co...

    December 18th, 2008 @ 07:22 PM