Forum: Ruby on Rails has_many :through

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5dd60445347fc3dec62474cc732fef0d?d=identicon&s=25 Kyle Mathews (Guest)
on 2006-01-19 16:08
(Received via mailing list)
Has anyone had much experience using the new has_many :though options
that are currently in Edge Rails? I'm looking at using it in my
project but haven't found much about it.

Kyle
2ccb2f7699267d258faaafce4ee997df?d=identicon&s=25 Cody Fauser (Guest)
on 2006-01-19 17:51
(Received via mailing list)
Kyle,

The :through option is the new support for join models.  This allows you
to do:

class Person < ActiveRecord::Base
  has_many :memberships
  has_many :organizations, :through => :memberships
end

class Membership < ActiveRecord::Base
  belongs_to :person
  belongs_to :organization
end

class Organization < ActiveRecord::Base
  has_many :memberships
  has_many :members, :class_name => 'Person', :through => :memberships
end

now you can do:
p = Person.find(:first)
p.organizations

or the opposite:
o = Organization.find(:first)
o.members

Also check out Rick Olson's tip:
http://rails.techno-weenie.net/tip/2005/12/23/teac...



Cody


On 1/19/06, Kyle Mathews <kyle@titaniumdev.com> wrote:
>
--
http://www.codyfauser.com
5dd60445347fc3dec62474cc732fef0d?d=identicon&s=25 Kyle Mathews (Guest)
on 2006-01-19 21:28
(Received via mailing list)
Thanks for the great response, that's just what I was looking for.

- Kyle
This topic is locked and can not be replied to.