# Has\_many through a habtm - what's the "source"?

**URL:** <https://www.ruby-forum.com/t/has-many-through-a-habtm-whats-the-source/116724>\
**Category:** Rails\
**Created:** [October 9, 2007, 1:09am UTC](https://www.ruby-forum.com/t/has-many-through-a-habtm-whats-the-source/116724 "2007-10-09T01:09:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [October 9, 2007, 1:09am UTC](https://www.ruby-forum.com/t/has-many-through-a-habtm-whats-the-source/116724/1 "2007-10-09T01:09:05Z")

</div>

Railsers:

Our classes use these associations:

[Frog]-1—_-[Adult]-_—\*-[Tadpole]

All adults are frogs, and each frog is mother or father to numerous  
tadpoles.

Declaring the right-side association is easy, via the hidden  
adults\_tadpoles table:

Tadpole.has\_and\_belongs\_to\_many :adults  
Adult.has\_and\_belongs\_to\_many :tadpoles

Declaring the -1–\* is easy too, when each Adult has a frog\_id:

Adult.belongs\_to :frog  
Frog.has\_many :adults

Now how do we add a collection of tadpoles to Frog? This attempt…

Frog.has\_many :tadpoles, :through =\> :adults, :source =\> :tadpoles

…invariably complains:

Invalid source reflection macro :has\_and\_belongs\_to\_many for  
has\_many :tadpoles,  
:through =\> :adults. Use :source to specify the source reflection.

So, what’s a “source reflection”?

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
^ assert\_xpath  
[O'Reilly Media - Technology and Business Training](http://tinyurl.com/yrc77g) ← assert\_latest Model

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [October 9, 2007, 3:12am UTC](https://www.ruby-forum.com/t/has-many-through-a-habtm-whats-the-source/116724/2 "2007-10-09T03:12:04Z")

</div>

Phlip wrote:

> Frog.has\_many :tadpoles, :through =\> :advlts, :source =\> :tadpoles
> 
> …invariably complains:
> 
> Invalid source reflection macro :has\_and\_belongs\_to\_many for  
> has\_many :tadpoles,  
> :through =\> :advlts. Use :source to specify the source reflection.
> 
> So, what’s a “source reflection”?

That’s some poor error message there. Dang. Anyway, what you’re  
attempting isn’t possible. With has\_many :through, you need a join model  
to go through. The source association is the association in the join  
model used as the source of the contents of the hm:t association itself  
(and the source reflectoin is that association’s reflection object).

has\_many :through habtm isn’t supported, though theoretically it’s just  
a Small Matter Of Programming.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
