Agile Web Development

Build it. Launch it. Love it.

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:


 class Pub < ActiveRecord::Base
   belongs_to :city

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

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

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

   # Now we go through a has_many :through association -
   # something that wasn't previously possible
   has_many :pubs, :through => :cities

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

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


License Rails' (MIT)
Rating (23 votes)
Owner Matt Westcott
Created 10 June 2007


  • Avatar
    James H
    19 July 2007

    This almost did what I needed it to. However, it exploded on a self-referential has_many :through.

  • Avatar
    21 July 2007

    James: You may have run into which is an existing Rails bug, albeit one that's a lot easier to stumble upon when you're making use of this plugin. Unfortunately it'd be difficult to roll that fix into this plugin while still keeping it stable with new Rails versions.

    Failing that - a full bug report would be very much appreciated.

  • Tobin
    29 July 2007

    Great plugin, works like a charm. I hope they will accept the patch for the next major release.

    Maybe you can even extend the plugin/patch so that you also can use :through with HABTM relationships (e.g. "has_many :news, through => :group", where :group is a habtm column).

  • Avatar
    3 January 2008

    Will this plugin work with rails 2.0?

  • Ryan Rempel
    6 January 2008

    Here is a tiny change that does at least part of what is needed to work with Rails 2.0. I haven't thoroughly tested.

    Index: nestedhasmany_through.rb

    --- nestedhasmany_through.rb
    +++ nestedhasmany_through.rb @@ -59,7 +59,7 @@ end

       def find(*args)
    • options = Base.send(:extractoptionsfrom_args!, args)
    • options = args.extract_options!

       conditions = construct_conditions
       if sanitized_conditions = sanitize_sql(options[:conditions])
  • Avatar
    Luke Grimstrup
    17 January 2008

    Champion. This did exactly what I wanted. It's no simple and the generated SQL is nice and tidy!

  • Avatar
    Matt White
    4 March 2008

    Absolutely fantastic... I was unaware that Rails couldn't handle this, but this made it work like a charm!

  • baddog
    24 April 2008

    I can't get this to install. That is, plugin says it installed it, but it doesn't run the rake tasks, and when I say plugin list, it doesn't show up. The files are in the plugin directory, tho. Any ideas? Is the require route OK for Rails 2.0.2?

  • baddog
    24 April 2008

    How about the reverse direction? That is, if I want to know the star system for a pub? I can chain it myself, of course, but it would be great for the association to exist, since maybe active_scaffold would pick it up then.

  • jhr
    28 August 2008

    Hmm, got an error:

    vendor/plugins/nestedhasmanythrough/lib/nestedhasmanythrough.rb:54: superclass mismatch for class HasManyThroughAssociation (TypeError)

  • Avatar
    29 October 2008

    Rails 2.1 breaks this plugin. In order to fix it, change line 54 of lib/nestedhasmany_through.rb to:

    class HasManyThroughAssociation < HasManyAssociation #:nodoc:

  • Avatar
    29 October 2008

    People using the old SVN repository (, please note that development of this plugin has now moved to Github:

    Most importantly, the version there is compatible with Rails 2.1 :-)

  • leo
    5 January 2009

    what about "vendor/plugins/nestedhasmanythrough/lib/nestedhasmanythrough.rb:54: superclass mismatch for class HasManyThroughAssociation (TypeError)" error has anyone resolve this? i use Rails 2.2.2

  • Avatar
    30 March 2009

    Any update on the error with Rails 2.2 as commented below ?

  • Avatar
    Bob Lail
    28 July 2009

    Perfect! Just what I was looking for!

Add a comment