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 produces invalid SQL

#2247

I've experienced a weird issue today which I'd like someone to confirm before I start working on a patch.

When using has_one :through with another has_one association like so:


  class Donor
    has_one  :primary_grouping, :class_name => "DonorGrouping", :conditions => { :primary => true }
    has_one  :primary_group, :through => :primary_grouping, :source => :donor_group
  
    has_many :donor_groupings, :conditions => { :primary => false }
    has_many :groups, :through => :donor_groupings, :source => :donor_group
  end

  class DonorGrouping < ActiveRecord::Base
    belongs_to :donor_group
    belongs_to :donor
  end

While the has_many association works flawlessly, a Donor.first.primary_group gives:


>> Donor.first.primary_group
ActiveRecord::StatementInvalid: PGError: ERROR:  column donor_groups.donor_id does not exist
LINE 1: ...CT "donor_groups".* FROM "donor_groups"    WHERE (donor_grou...
                                                             ^
: SELECT "donor_groups".* FROM "donor_groups"    WHERE (donor_groups.donor_id = 18 )

Where the requested donor_id of 18 is the id of Donor.first..

This could also be related to #1167 which has never been resolved complete as it seems.

Pretty severe issue, isn't it?!

Reported by Pascal Ehlert · March 16th, 2009 @ 06:42 AM

State: resolved
Milestone: 3.x
Assigned to: Aaron Patterson Aaron Patterson
Importance: none

Activity

  1. njakobsen
    njakobsen

    I have the same problem in rails 2.3.2

    
    class Item
    has_one :default_digital_object, :through => :item_digital_objects, :source => :digital_object
    end
    

    Item.first.default_digital_object produces the following SQL:

    @@@SELECT "digital_objects".* FROM "digital_objects" WHERE (digital_objects.item_id = 14891 )@@@

    It seems to be ignoring the :through parameter.

    March 31st, 2009 @ 01:37 AM

  2. Joshua White
    Joshua White

    I'm having the same issue on 2.3.2 ... I can't get has_one through on a belongs_to relationsbip

    ExamRecord.rb (contains patient_id )

    belongs_to :patient
    has_one :history, :through => :patient
    has_many :insurance_providers, :through => :patient

    Patient.rb
    has_many :exam_records
    has_many :insurance_providers
    has_one :history

    History.rb (contains patient_id)
    belongs_to :patient

    InsuranceProvider.rb
    belongs_to :patient

    So on the console if I do

    ExamRecord.first.insurance_providers, I get my array of insurance providers fine...

    ExamRecord.first.history I get

    ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_one for has_many :history, :through => :patient. Use :source to specify the source reflection.

        from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/reflection.rb:301:in `check_validity!'
        from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/has_many_through_association.rb:5:in `initialize'
        from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1230:in `new'
        from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1230:in `history'
        from (irb):29
    

    I have tried specifying :source => :history and I get

    ExamRecord.first.history ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_one for has_many :history, :through => :patient. Use :source to specify the source reflection.

        from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/reflection.rb:301:in `check_validity!'
        from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/has_many_through_association.rb:5:in `initialize'
        from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1230:in `new'
        from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1230:in `history'
        from (irb):31
    

    Any Ideas?

    June 4th, 2009 @ 12:01 AM

  3. Joshua White
    Joshua White

    Apparently this is discussed in #1149, the has_one through only works when the join is a has_many or belongs_to.

    June 4th, 2009 @ 12:04 AM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  5. Jon Leighton
    Jon Leighton
    • Assigned user set to Aaron Patterson
    • Importance changed from to

    This is no longer an issue can can be closed. The behaviour is verified by the test test_has_one_through_with_has_one which exists in test/cases/associations/has_one_through_associations_test.rb and already passes. Thanks.

    December 12th, 2010 @ 01:56 PM

  6. Aaron Patterson
    Aaron Patterson
    • State changed from new to resolved

    Closing per Jon's comment.

    December 15th, 2010 @ 07:19 PM

  7. bingbing
  8. bingbing