This project is archived and is in readonly mode.

#1507 ✓resolved
Matt Jones

preloading has_one :through on a belongs_to

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

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

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Referenced by

Pages