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

Ticket #4756 (closed enhancement: fixed)

Opened 2 years ago

Last modified 4 months ago

Build support for has_one_through associations

Reported by: anonymous Assigned to: technoweenie@gmail.com
Priority: high Milestone:
Component: ActiveRecord Version: edge
Severity: normal Keywords: has_one through
Cc: tomafro, asanghi, shoe, thechrisoshow

Description

I have:

class Person < ActiveRecord::Base

has_many :addressables has_many :addresses, :through => :addressables

end

class Address < ActiveRecord::Base

has_many :addressables has_many :people, :through => :addressables

end

The addressables table also has a column to designate whether the address is the primary address for that person so the need arises for a relationships like:

class Person < ActiveRecord::Base

has_one :primary_address. :through => :addressables, :conditions => "primary = '1'"

end

I'd be awesome to have that.

Attachments

has_one_through.diff (4.2 kB) - added by thechrisoshow on 03/22/08 01:17:50.
tests no longer rely on implicit ordering (so pass with PostgreSQL)

Change History

(follow-up: ↓ 3 ) 04/15/06 21:09:20 changed by rick

  • owner changed from David to technoweenie@gmail.com.

Someday soon...

04/16/06 07:17:15 changed by anonymous

I'm not sure if "Someday soon..." means you're making fun of it or you're being serious. If you're serious it seems kind of vague to say "Someday soon..."

(in reply to: ↑ 1 ) 11/16/06 14:46:24 changed by akruth

Will this be supported somewhen? I'm missing this. Is there a workaround?

03/16/07 09:53:11 changed by tomafro

  • cc set to tomafro.
  • keywords set to has_one through.

05/25/07 06:34:16 changed by danger

@technoweenie: Has there been any talk about a has_one :through ? It seems the popular convention has been to manually define a one-line method instead. It's a hack, but it seems to work fine. Does anyone in core seem itching to add this association?

05/29/07 05:45:45 changed by josh

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

I've seen a few tickets requesting this type of feature. Although, it doesn't seem important enough for any of them to write a patch for it.

Please reopen once we have a patch to review.

02/17/08 19:51:15 changed by asanghi

  • cc changed from tomafro to tomafro, asanghi.

02/18/08 21:43:52 changed by wdiechmann

  • priority changed from normal to high.
  • status changed from closed to reopened.
  • version set to edge.
  • resolution deleted.

The argument of, not implementing this because one-liner hacks are afloat, is of cause valid, but so is, I'd like to believe, the original ticket, and - may I add - modeling the real world will have you see this construct in a great many places.

Preferred contact amongst contacts

Preferred way of communication with a person amongst legend

and to not further ends, I could go on


Anyways, I trembled over this: http://artofmission.com/articles/2007/6/21/has_one-through

Turns out, someone actually did take a stab at implementing the bugger, after all :)

I'm not fluent in "patchuese" so - I'll just reopen it, hoping for someone with a lot more patch_fu on his/her hands to take this one up the mountain and nail the son..

03/09/08 21:01:08 changed by thechrisoshow

I've bunged together a patch with code shamelessly nicked from here: http://artofmission.com/articles/2007/6/21/has_one-through and here: http://idm.s9.xrea.com/ratio/2006/08/04/000496.html

In order to fulfill the requirements outlined in the description of this ticket you would implement the following:

class Person < ActiveRecord::Base
  has_one :primary_address, :through => :addressables, :conditions => ["addressable.primary = ?", true], :source => :addressable
end

03/14/08 15:22:17 changed by thechrisoshow

Updated to simplify HasOneThroughAssociation (now inherits from HasManyThroughAssociation).

Also the through record is automatically created i.e.

class Member
  has_one :membership
  has_one club, :through => :membership
end

banana_man = Member.create
banana_man.club = Club.find_by_name("FLA")
#banana_man.membership automatically created

03/14/08 16:05:19 changed by thechrisoshow

Latest patch ensures that replace works i.e.:

banana_man.club = Club.find_by_name("Banana fans")

banana_man.club = Club.find_by_name("Some other club")

03/14/08 16:39:01 changed by shoe

  • cc changed from tomafro, asanghi to tomafro, asanghi, shoe.

I think this should include a test for the ":as" case.

03/15/08 07:53:03 changed by thechrisoshow

Patch uploaded now includes test for polymorphic has_one :through

03/17/08 14:40:41 changed by thechrisoshow

  • cc changed from tomafro, asanghi, shoe to tomafro, asanghi, shoe, thechrisoshow.

03/21/08 18:09:08 changed by rick

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [9067]) Add has_one :through support, finally. Closes #4756 [thechrisoshow]

03/21/08 23:15:24 changed by bitsweat

(In [9073]) Fix schema.rb some more. sponsors.sponsorable_type should be a string. References #4756.

03/22/08 01:17:50 changed by thechrisoshow

  • attachment has_one_through.diff added.

tests no longer rely on implicit ordering (so pass with PostgreSQL)

03/22/08 02:20:40 changed by bitsweat

(In [9075]) has_one :through supports :source_type. Fix up some tests. References #4756.