It looks like you are trying to create a self-referential has_many
:through association, which should work just fine for what you want.
However you don’t have it set up properly. The :through option on a
has_many association must refer to another has_many association in the
same model. It gets to its target through the other association, thus
Here’s an example snipped from the Rails 1.1 test code (always a good
place to look for example code):
class Author < ActiveRecord::Base
has_many :favorite_authors, :through => :author_favorites, :order =>
class AuthorFavorite < ActiveRecord::Base
belongs_to :favorite_author, :class_name => “Author”, :foreign_key =>
If you need to change the name of the association from its name in the
join model, use the :source option to specify that name. Check the docs
I should blog an article on this soon, as it can get a bit tricky to
combine all the options and get them to work together correctly.
Thomas Counsell wrote:
I’d like to use :through to create a web of associations like:
class Thing < ActiveRecord::Base
has_many :child_things, :through => :thing_thing
has_many :parent_things, :through => :thing_thing, :some_other_option?
class ThingThing < ActiveRecord::Base
belongs_to :child_thing, :class_name => ‘Thing’, :foreign_key =>
The child_things works. The parent_things does not, or at least, I
cannot figure out the sytnax. I would like it to issue an sql like:
SELECT things.* FROM things INNER JOIN thing_things ON things.id =
thing_things.thing_id WHERE (thing_things.child_thing_id = 6201)
Is there a way to trigger this sql using the through options?