Help with SQL -> associations (Multiple JOINs)

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

On Saturday, October 24, 2009, Gleb M. [email protected]
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.

Matt Haley wrote:

On Saturday, October 24, 2009, Gleb M. [email protected]
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs