Rails 3.x gem that allows has_many :through to go through other has_many :throughs
Ruby
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
Gemfile
Gemfile.lock
MIT-LICENSE
README.rdoc
Rakefile
init.rb
nested_has_many_through.gemspec

README.rdoc

NestedHasManyThrough

This gem 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).

NOTE: Rails 3.x supports only. Rails 2.3 users might want to try the original nested_has_many_through plugin

Intallation

Install the gem:

gem install nested_has_many_through

Add it to your gem file

gem “nested_has_many_through”

Usage

class Author < User
  has_many :posts
  has_many :categories, :through => :posts, :uniq => true
  has_many :similar_posts, :through => :categories, :source => :posts
  has_many :similar_authors, :through => :similar_posts, :source => :author, :uniq => true
  has_many :posts_of_similar_authors, :through => :similar_authors, :source => :posts, :uniq => true
  has_many :commenters, :through => :posts, :uniq => true
end

class Post < ActiveRecord::Base
  belongs_to :author
  belongs_to :category
  has_many :comments
  has_many :commenters, :through => :comments, :source => :user, :uniq => true
end

The first two has_manys of Author are plain vanilla, the last four are what this plugin enables

# has_many through a has_many :through
has_many :similar_posts, :through => :categories, :source => :posts

# doubly nested has_many :through
has_many :similar_authors, :through => :similar_posts, :source => :author, :uniq => true

# whoah!
has_many :posts_of_similar_authors, :through => :similar_authors, :source => :posts, :uniq => true

# has_many through a has_many :through in another model
has_many :commenters, :through => :posts, :uniq => true

Maintainers

Copyrights

This gem based on code nested_has_many_through plugin

MIT License. Copyright © 2008 Ian White - ian.w.white@gmail.com