This project is archived and is in readonly mode.
has_one :through produces invalid SQL
Reported by Pascal Ehlert | March 16th, 2009 @ 06:42 AM | in 3.x
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?!
Comments and changes to this ticket
-
njakobsen March 31st, 2009 @ 01:37 AM
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.
-
Joshua White June 4th, 2009 @ 12:01 AM
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 => :patientPatient.rb
has_many :exam_records
has_many :insurance_providers
has_one :historyHistory.rb (contains patient_id)
belongs_to :patientInsuranceProvider.rb
belongs_to :patientSo 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?
-
Joshua White June 4th, 2009 @ 12:04 AM
Apparently this is discussed in #1149, the has_one through only works when the join is a has_many or belongs_to.
-
Jon Leighton December 12th, 2010 @ 01:56 PM
- 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 intest/cases/associations/has_one_through_associations_test.rb
and already passes. Thanks. -
Aaron Patterson December 15th, 2010 @ 07:19 PM
- State changed from new to resolved
Closing per Jon's comment.
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>