From: Matt H Date: 2009-10-25T05:34:39+09:00 Subject: Re: Help with SQL -> associations (Multiple JOINs) On Saturday, October 24, 2009, Gleb Mazovetskiy 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. -- Find me - http://www.smajn.net/social