Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #4996 (reopened enhancement)

Opened 4 years ago

Last modified 2 years ago

[PATCH] has_many :through should work with a has_many -> has_one relationship

Reported by: ryani.mail@gmail.com Assigned to: David
Priority: normal Milestone:
Component: ActiveRecord Version:
Severity: normal Keywords: PATCH
Cc: chewi, tomafro

Description

Test case that I want to work:

class Thing < ActiveRecord::Base
  has_many :instances
  has_many :offers, :through => :instances
end

class Instance < ActiveRecord::Base
  has_one :offer # potentially nil if no matching element in offers table
end

class Offer < ActiveRecord::Base
  belongs_to :instance
end

Thing.find(:first).offers # => array of all offers on instances of thing

As it is, I was getting the following error:

ActiveRecord::HasManyThroughSourceAssociationMacroError: ActiveRecord::HasManyThroughSourceAssociationMacroError
        from c:/InstantRails-1.0/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/reflection.rb:181:in `check_validity!'
        from c:/InstantRails-1.0/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations/has_many_through_association.rb:6:in `initialize'
        from c:/InstantRails-1.0/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations.rb:876:in `offers'
        from (irb):93

has_one queries through the same SQL as has_many, just with the restriction that each object only has one. So, hopefully, this is an easy change for someone who gets the innards of :through.

Attachments

through-has-one-1.23.patch (1.2 kB) - added by chewi on 08/20/07 22:20:10.

Change History

05/29/07 05:43:46 changed by josh

  • status changed from new to closed.
  • resolution set to duplicate.

08/20/07 22:19:39 changed by chewi

  • keywords set to PATCH.
  • status changed from closed to reopened.
  • resolution deleted.
  • summary changed from has_many :through should work with a has_many -> has_one relationship to [PATCH] has_many :through should work with a has_many -> has_one relationship.

This actually isn't a duplicate. #4756 wants has_one :through. This ticket wants to do has_many :through, using a has_one as the source. Totally different things!

As it turns out, you only need to change two lines and one of those was just a line that was checking to make sure you didn't try it, the same line that caused the aforementioned error. Without further ado, here's a small patch against 1.23.

08/20/07 22:20:10 changed by chewi

  • attachment through-has-one-1.23.patch added.

08/28/07 15:16:05 changed by chewi

  • cc set to chewi.

08/28/07 18:55:33 changed by lifofifo

The patch needs tests

10/03/07 08:50:57 changed by tomafro

  • cc changed from chewi to chewi, tomafro.

11/15/07 21:44:52 changed by Nolgthorn

Patch doesn't work when has_one source is referenced by polymorphic model.

11/15/07 21:51:54 changed by chewi

Heh I was made to stop using polymorphic associations. Just so I'm clear, can you give an example?