Plugins - Nested has_many_through
Add to favoritesThis 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 # Now we go through a has_many :through association - # something that wasn't previously possible has_many :pubs, :through => :cities end 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 end

Hmm, got an error:
vendor/plugins/nestedhasmanythrough/lib/nestedhasmanythrough.rb:54: superclass mismatch for class HasManyThroughAssociation (TypeError)
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.
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?
Absolutely fantastic... I was unaware that Rails couldn't handle this, but this made it work like a charm!
Champion. This did exactly what I wanted. It's no simple and the generated SQL is nice and tidy!
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
options = args.extract_options!
Will this plugin work with rails 2.0?
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).
James: You may have run into http://dev.rubyonrails.org/ticket/8267 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.
This almost did what I needed it to. However, it exploded on a self-referential has_many :through.