<p>I have a two tables joined with a join table - this is just pseudo code:</p>

<pre><code>Library
Book
LibraryBooks
</code></pre>

<p>What I need to do is if i have the id of a library, i want to get all the libraries that all the books that this library has are in.</p>

<p>So if i have Library 1, and Library 1 has books A and B in them, and books A and B are in Libraries 1, 2, and 3, is there an elegant (one line) way todo this in rails?</p>

<p>I was thinking:</p>

<pre><code>l = Library.find(1)
allLibraries = l.books.libraries
</code></pre>

<p>But that doesn't seem to work. Suggestions?</p>

Perhaps:
```
```
l.books.map {|b| b.libraries}
```
```
or
```
```
l.books.map {|b| b.libraries}.flatten.uniq
```
```
if you want it all in a flat array.
Of course, you should really define this as a method on Library, so as to uphold the noble cause of encapsulation.