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_one :through on new record doesn't query for associated object

#5854

Given:

class User < AR
  has_one :user_info
  has_many :bank_infos
end
class UserInfo < AR
  belongs_to :user
end
class BankInfo < AR
  belongs_to :user
  has_one :user_info, :through => :user
end

I do:

bank_info = user.bank_infos.build
bank_info.user_info

where user has associated user_info. And I expect bank_info.user_info to equal user.user_info, however it is nil.

Attaching patch...
Any feedback would be appreciated.

Reported by Alexey Ilyichev · October 22nd, 2010 @ 03:12 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Alexey Ilyichev
    Alexey Ilyichev

    Sorry for weird formatting. Code from previous post

    class User < AR
      has_one :user_info
      has_many :bank_infos
    end


    class UserInfo < AR belongs_to :user end


    class BankInfo < AR belongs_to :user has_one :user_info, :through => :user end

    October 22nd, 2010 @ 04:24 PM

  2. Jon Leighton
    Jon Leighton
    • State changed from new to invalid
    • Importance changed from to Low

    Hi there,

    Thanks for the bug report. To do this you can use the :inverse_of option on your User.has_many :bank_infos.

    Jon

    December 21st, 2010 @ 07:57 PM

  3. Alexey Ilyichev
    Alexey Ilyichev

    Hi, Jon.

    Thanks for the reply. Inverse_of does not solve anything in this case.

    January 19th, 2011 @ 02:13 PM

  4. Jon Leighton
    Jon Leighton

    Hi,

    You're right, inverse_of is irrelevant here. However I have just tried the test case you provided against latest master and it passes.

    Thanks

    January 23rd, 2011 @ 09:41 PM