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.

Invalid has_one :through source_reflection macros

#1149

In (http://api.rubyonrails.org/class... "has_one doc"), it says:

:through: Specifies a Join Model through which to perform the query. Options for :class_name and :foreign_key are ignored, as the association uses the source reflection. You can only use a :through query through a has_one or belongs_to association on the join model.

But using a has_one association on the join model throws a ActiveRecord::HasManyThroughSourceAssociationMacroError, /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/reflection.rb:211

What's wrong? Documentation or code?

Reported by Antonio Tapiador · October 1st, 2008 @ 02:03 PM

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

Activity

  1. Pratik
    Pratik
    • State changed from new to invalid

    This was recently fixed iirc. Could you please try on edge and provide a failing test case in AR test suite if it doesn't work ?

    Thanks.

    January 5th, 2009 @ 05:09 PM

  2. sbwoodside
    sbwoodside
    • Assigned user set to Pratik

    The documentation now says:

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassM...

    You can only use a :through query through a belongs_to or has_many association on the join model.

    So, apparently the docs were wrong.

    Also note, old bug: http://dev.rubyonrails.org/ticket/4996

    May 26th, 2009 @ 04:47 AM

  3. sbwoodside
    sbwoodside
    • Assigned user cleared.

    Whoops, sorry didn't mean to change responsible. Anyway, since the documentation is correct now, I guess this bug should be closed.

    May 26th, 2009 @ 04:48 AM

  4. Joshua White
    Joshua White

    Is there a good way to do this in the case that the join model is a has_one relationship? Or is writing SQL the best way to do it?

    June 4th, 2009 @ 12:03 AM

  5. Joshua White
    Joshua White

    I tried a number of things and eventually realized I could just specify the foreign_key
    so in my
    History.rb
    belongs_to :patient

    Patient.rb
    has_many :exam_records
    has_one :history

    ExamRecord.rb
    belongs_to :patient
    has_one :history, :foreign_key => 'patient_id'

    On the shell I'm finally able to say
    ExamRecord.first.history

    June 4th, 2009 @ 12:22 AM

  6. Joshua White
    Joshua White

    Sorry, I realized my last post was incorrect, it was comparing exam_record.id to history.patient_id, and that this bug is closed, I just had to specify the primary key and all was well!

    ExamRecord.rb belongs_to :patient has_one :history, :foreign_key => 'patient_id', :primary_key => 'patient_id'

    June 4th, 2009 @ 12:54 AM