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: 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

Vitals

Repository http://code.torchbox.com/svn/rails/plugins/nested_has_many_through/
License Rails' (MIT)
Rating (23 votes)
Owner Matt Westcott
Created 10 June 2007

Comments

  • 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 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.

  • 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
    vince
    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 (http://code.torchbox.com/svn/rails/plugins/nestedhasmanythrough/), please note that development of this plugin has now moved to Github: http://github.com/ianwhite/nestedhasmanythrough/tree/master

    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
    Observer
    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