# Help with SQL -\> associations (Multiple JOINs)

**URL:** <https://www.ruby-forum.com/t/help-with-sql-associations-multiple-joins/177831>\
**Category:** Ruby\
**Created:** [October 24, 2009, 9:14pm UTC](https://www.ruby-forum.com/t/help-with-sql-associations-multiple-joins/177831 "2009-10-24T21:14:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [October 24, 2009, 9:14pm UTC](https://www.ruby-forum.com/t/help-with-sql-associations-multiple-joins/177831/1 "2009-10-24T21:14:58Z")

</div>

Is there a way to express this with associations?

```
@songs = Song.find_by_sql(%Q{
        SELECT songs.*, song_playlist_relationships.playlist_id
        FROM songs INNER JOIN song_playlist_relationships
          ON songs.id = song_playlist_relationships.song_id
          WHERE song_playlist_relationships.playlist_id IN

```

(#{@playlists.collect{|p| p.id}.join(’,’)})})

Thank you,  
Gleb

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [October 24, 2009, 10:35pm UTC](https://www.ruby-forum.com/t/help-with-sql-associations-multiple-joins/177831/2 "2009-10-24T22:35:48Z")

</div>

On Saturday, October 24, 2009, Gleb M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is there a way to express this with associations?
> 
> ```
> @songs = Song.find_by_sql(%Q{
> SELECT songs.*, song_playlist_relationships.playlist_id
> FROM songs INNER JOIN song_playlist_relationships
> ON songs.id = song_playlist_relationships.song_id
> WHERE song_playlist_relationships.playlist_id IN
> 
> ```
> 
> (#{@playlists.collect{|p| p.id}.join(‘,’)})})

Something like this should work. Double-check the :joins option in the  
Rails documentation. I might have it wrong.

playlist\_ids = @playlists.collect{|p| p.id}  
Song.find(:all,  
:conditions =\> [“song\_playlists\_relationships.playlist\_id IN (?)”,  
playlist\_ids],  
:joins =\> :song\_playlist\_relationships)

–  
Sent via mobile.

---

<div class="post-metadata">

**Author:** ![glex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f07891/32.png) [@glex](https://www.ruby-forum.com/u/glex)\
**Post date:** [October 25, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/help-with-sql-associations-multiple-joins/177831/3 "2009-10-25T17:45:50Z")

</div>

Matt Haley wrote:

> On Saturday, October 24, 2009, Gleb M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Is there a way to express this with associations?
> > 
> > ï¿½ ï¿½@songs = Song.find\_by\_sql(%Q{  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½SELECT songs.\*, song\_playlist\_relationships.playlist\_id  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½FROM songs INNER JOIN song\_playlist\_relationships  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ON songs.id = song\_playlist\_relationships.song\_id  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½WHERE song\_playlist\_relationships.playlist\_id IN  
> > (#{@playlists.collect{|p| p.id}.join(‘,’)})})
> 
> Something like this should work. Double-check the :joins option in the  
> Rails documentation. I might have it wrong.
> 
> playlist\_ids = @playlists.collect{|p| p.id}  
> Song.find(:all,  
> :conditions =\> [“song\_playlists\_relationships.playlist\_id IN (?)”,  
> playlist\_ids],  
> :joins =\> :song\_playlist\_relationships)
> 
> –  
> Sent via mobile.

Thank you, I’ll look into it!

Cheers,

Gleb
