This project is archived and is in readonly mode.

#1149 ✓invalid
Antonio Tapiador

Invalid has_one :through source_reflection macros

Reported by Antonio Tapiador | October 1st, 2008 @ 02:03 PM | in 2.x

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?

Comments and changes to this ticket

  • Pratik

    Pratik January 5th, 2009 @ 05:09 PM

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

  • sbwoodside

    sbwoodside May 26th, 2009 @ 04:47 AM

    • 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

  • sbwoodside

    sbwoodside May 26th, 2009 @ 04:48 AM

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

  • Joshua White

    Joshua White June 4th, 2009 @ 12:03 AM

    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?

  • Joshua White

    Joshua White June 4th, 2009 @ 12:22 AM

    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

  • Joshua White

    Joshua White June 4th, 2009 @ 12:54 AM

    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'

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

Referenced by

Pages