Assuming rails app has the following models song, play, radio_station: song has_many plays play has_many radio_stations radio_station has attribute "call_sign"
I want to create one find for Song that would return for me the all songs where the last play (of the song) was played by a radio station of "WKRP"
I could do
found = [] Song.find(:all,:include=>[{:plays=>[:radio_station]}]).each do |song| found << song if song.plays.last.radio_station.call_sign == "WKRP" end
but this would mean that all songs must be pulled down from the DB first and looped through... which would get slow as the # of songs and plays builds.
How can I put this into one find condition?
It seems like this is something that should be doable - but I can't figure out how and I am not really an SQL guru ...
and then.. would love to squeeze it into a named scope so i could call it like:
Song.find_all_last_played_at("WKRP")