2012-12-17 23 views
0

我將此查詢集中在一起,表現良好。嘗試使用rails執行連接表時發生輕微查詢問題2.3.5

 search = Artist.where(:status => "active").where("upcoming_events_count > 0").order("echonest_external_popularity DESC").limit(limit)) 

問題是我想添加一個檢查,這是在另一個表ArtistPhoto上。比賽需要確保藝術家(來自artist_id)也包含有效的照片。

我有查詢工作獨立發現:

 ArtistPhoto.where("artist_id = ? and artist_photos.primary = ?", self.id, true).first 

如果任何人都可以得到連接成1個查詢將是巨大的幫助。

感謝

回答

2

使用joins添加其他關聯關係:

search = Artist.joins(:artist_photos). 
       where("artist_photos.primary = ?", true). 
       where(...) # other filters here 

這個答案假定您在Artist模型有has_many :artist_photos

+0

工作完美。正是我需要的,謝謝Pinny! – stonep

相關問題