Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Nested Has Many Through

Nested Has Many Through

This plugin makes it possible to define has_many :through relationships that go through other has_many :through relationships, possibly through an arbitrarily deep hierarchy. This allows associations across any number of tables to be constructed, without having to resort to find_by_sql (which isn’t a suitable solution if you need to do eager loading through :include as well).

It is hoped that this feature will in time be applied to the Rails core, after which this plugin will become unnecessary. See: http://dev.rubyonrails.org/ticket/6461

Example

class Pub < ActiveRecord::Base belongs_to :city end

class City < ActiveRecord::Base belongs_to :country has_many :pubs end

class Country < ActiveRecord::Base belongs_to :planet has_many :cities has_many :pubs, :through => cities end

class Planet < ActiveRecord::Base belongs_to :star_system has_many :countries has_many :cities, :through => :countries end

  1. Now we go through a has_many :through association -
  2. something that wasn’t previously possible has_many :pubs, :through => :cities

class StarSystem < ActiveRecord::Base has_many :planets has_many :countries, :through => :planets end

  1. We can also use a has_many :through association for the source
  2. association; in this case, Country#pubs has_many :pubs, :through => countries

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all details


Membership

+ Join this railsplugin

Record Maintainer

'None'