# Has\_many through habtm alternatives?

**URL:** <https://www.ruby-forum.com/t/has-many-through-habtm-alternatives/127476>\
**Category:** Rails\
**Created:** [January 29, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/has-many-through-habtm-alternatives/127476 "2008-01-29T17:23:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gsterndale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@gsterndale](https://www.ruby-forum.com/u/gsterndale)\
**Post date:** [January 29, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/has-many-through-habtm-alternatives/127476/1 "2008-01-29T17:23:22Z")

</div>

In the following hypothetical scenario, I’d like to associate Books  
and Clubs.

Class Club \< ActiveRecord::Base  
has\_many :memberships  
has\_many :readers, :through =\> :memberships

```
        #DOES NOT WORK
        #has_many :books, :through => :readers

        #DOES NOT ADD find_in_collection METHODS
        #has_many :books, :finder_sql =>
        # %q{
        # select distinct books.*
        # from books
        # join memberships on books.reader_id

```

= memberships.reader\_id  
# and memberships.club\_id =  
#{self.id}  
# order by books.read\_at desc  
# }  
end

Class Memberships \< ActiveRecord::Base  
belongs\_to :club  
belongs\_to :reader  
end

Class Reader \< ActiveRecord::Base  
has\_many :memberships  
has\_many :books  
end

Class Book \< ActiveRecord::Base  
belongs\_to :reader  
end

If Memberships were simply a mapping table (clubs\_readers) and class  
Club has\_and\_belongs\_to\_many :readers, I still couldn’t specify that  
class Club has\_many :books, :through =\> :readers because you can’t  
associate through habtm.

Unfortunately, associating books through readers in class Club does  
not work because readers is associated through memberships.  
Apparently, multiple levels of indirection is not supported.

If you use finder\_sql, all the find\_in\_collection methods are not  
added to the association proxy. Without which I won’t be able to call  
my\_club.books.find(:all, :conditions =\> ‘is\_read = true’) or to do  
pagination for example.

Does anyone have any idea how I can create this association without  
finder\_sql and perhaps extending the association proxy?

Thanks!

---

<div class="post-metadata">

**Author:** ![gsterndale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@gsterndale](https://www.ruby-forum.com/u/gsterndale)\
**Post date:** [January 31, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/has-many-through-habtm-alternatives/127476/2 "2008-01-31T16:28:04Z")

</div>

Bump

---

<div class="post-metadata">

**Author:** ![gsterndale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@gsterndale](https://www.ruby-forum.com/u/gsterndale)\
**Post date:** [February 2, 2008, 8:09am UTC](https://www.ruby-forum.com/t/has-many-through-habtm-alternatives/127476/3 "2008-02-02T08:09:21Z")

</div>

gsterndale wrote:

> In the following hypothetical scenario, I’d like to associate Books  
> and Clubs.  
> […]  
> If Memberships were simply a mapping table (clubs\_readers) and class  
> Club has\_and\_belongs\_to\_many :readers, I still couldn’t specify that  
> class Club has\_many :books, :through =\> :readers because you can’t  
> associate through habtm.
> 
> Unfortunately, associating books through readers in class Club does  
> not work because readers is associated through memberships.  
> Apparently, multiple levels of indirection is not supported.
> 
> If you use finder\_sql, all the find\_in\_collection methods are not  
> added to the association proxy. Without which I won’t be able to call  
> my\_club.books.find(:all, :conditions =\> ‘is\_read = true’) or to do  
> pagination for example.
> 
> Does anyone have any idea how I can create this association without  
> finder\_sql and perhaps extending the association proxy?
> 
> Thanks!

Your right, the multi-pass :through statements are not supported. It’s  
messy enough as it is with just a single through. I really don’t have a  
good answer, and I think the silence may be because no one else has come  
up with one either. But this has been sitting here a while, and I  
thought it would be polite to at least let you know I looked at it. My  
only thought is to write a few functions in Club like ‘read\_books’ or  
‘unread\_books’ and drop back to SQL under the covers, but that is not a  
good solution, just a hack.

---

<div class="post-metadata">

**Author:** ![gsterndale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@gsterndale](https://www.ruby-forum.com/u/gsterndale)\
**Post date:** [February 9, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/has-many-through-habtm-alternatives/127476/4 "2008-02-09T18:24:49Z")

</div>

Thanks Starr. I appreciate the thought. I’ll update this thread if I  
find a solution. Thanks again.

On Feb 2, 2:09 am, Starr T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
