Plugins - Nested has_many_through

StarAdd to favorites

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

Matt Westcott

http://code.torchbox.com/svn/rails/plugins/nested_has_many_through/

Rails' (MIT)

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Model

Tags

Comments

Add a comment
jhr 28 Aug 2008

Hmm, got an error:

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

baddog 24 Apr 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.

baddog 24 Apr 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?

Matt White 4 Mar 2008

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

Luke Grimstrup 17 Jan 2008

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

Ryan Rempel 6 Jan 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])
    
vince 3 Jan 2008

Will this plugin work with rails 2.0?

Tobin 29 Jul 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).

Matt Westcott 21 Jul 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.

James H 19 Jul 2007

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

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.

Have a comment?