# Using "find" when you have 2 has\_many relations

**URL:** <https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652>\
**Category:** Rails\
**Created:** [January 17, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652 "2006-01-17T16:31:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 17, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652/1 "2006-01-17T16:31:58Z")

</div>

An Account has\_many Websites which in turn has\_many WebsiteDomains

Now I can of course do this:  
@domains = Account.find(1).websites.find(1).website\_domains.find(:all)  
To get all the domains for Account with id 1 and Website with id 1.

I would like to do something like this though:  
@domains =  
Account.find(1).websites.find(:all).website\_domains.find(:all)  
IE, get ALL domains for all websites in Account 1  
The code above doesn’t work because of the multiple find(:all)

I could do it using :joins and :conditions I guess but then I lose other  
useful stuff like attributes matching column names in the database.

Any ideas?

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 17, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652/2 "2006-01-17T16:46:04Z")

</div>

@domains = Domain.find(:all, :conditions =\> [“websites.id = ? AND  
websites.account\_id = ?”, website\_id, acount\_id], :include =\> “website”)

Should do it. May need to be tweaked a little.

-Nick

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 17, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652/3 "2006-01-17T17:13:03Z")

</div>

Oops, I forgot that I actually had one more relation that has to go in  
there.  
Watcher belongs\_to WebsiteDomain.

So what I really would like to do is something like this:

@watcher = Watcher.find(:all, :conditions =\> [websites.account\_id = ?",  
acount\_id], :include =\> [“website\_domain”, “website”])

This does not work however and I just found this on the RoR API site:  
“Itâ??s currently not possible to use eager loading on multiple  
associations from the same table.”

Is that why I can not do this?

If so, any work-arounds for it?

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 17, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652/4 "2006-01-17T20:44:00Z")

</div>

On 1/17/06, Per Dj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “It’s currently not possible to use eager loading on multiple  
> associations from the same table.”
> 
> Is that why I can not do this?

Because Rails wasn’t designed with it in mind (it doesn’t alias tables  
in the SQL), and patches to implement it have been rejected (see  
[http://dev.rubyonrails.org/ticket/1562](http://dev.rubyonrails.org/ticket/1562)).

> If so, any work-arounds for it?

There’s the Allow Multiple Associations Same Table plugin:  
[http://wiki.rubyonrails.org/rails/pages/Allow+Multiple+Associations+Same+Table+Plugin](http://wiki.rubyonrails.org/rails/pages/Allow+Multiple+Associations+Same+Table+Plugin),  
which is a conversion of the latest patch in the ticket mentioned  
above.

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 17, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652/5 "2006-01-17T19:38:27Z")

</div>

The goal is to get all Watchers for one Account.  
So my main problem is that since I have so many relations (Account --\>  
Websites --\> WebsiteDomains --\> Watchers) in between those two I’m not  
sure how to code it in any elegant way.

Thanks for your help, really appreciate it!

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 17, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652/6 "2006-01-17T19:31:37Z")

</div>

I guess it comes down to what you are looking for in the final results.  
Whats the end goal? A list of domains per watcher? per website? per  
account?  
Need some more info to be able to refine it a bit more.

-Nick

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 17, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652/7 "2006-01-17T22:49:09Z")

</div>

That’s a surprise 😮  
Thanks for the info.

So if I want to avoid plugins and engines etc then my only choice is to  
raw sql with find\_by\_sql ?

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 18, 2006, 3:52am UTC](https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652/8 "2006-01-18T03:52:27Z")

</div>

Looks to be the easiest way yes, but its not to painful is it?

Watcher.find\_by\_sql([“SELECT w.\* FROM watchers w LEFT JOIN  
website\_domains ON website\_domains.id = w.website\_domain\_id LEFT JOIN  
websites ON websites.id = website\_domains.website\_id WHERE  
websites.account\_id = ?”, account\_id])

Should do it if I got the table/column names right…

---

<div class="post-metadata">

**Author:** ![pernod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f14d63/32.png) [@pernod](https://www.ruby-forum.com/u/pernod)\
**Post date:** [January 18, 2006, 7:43am UTC](https://www.ruby-forum.com/t/using-find-when-you-have-2-has-many-relations/52652/9 "2006-01-18T07:43:44Z")

</div>

> Looks to be the easiest way yes, but its not to painful is it?  
> No, not too painful 🙂  
> Thanks for your help!
