2012-02-26 26 views
0

我有一個ForumForumTag HABTM的關係。我也有一個名爲@tags的變量數組。這個數組包含我用來過濾論壇的一些ForumTag的名字。我目前正在查詢,以獲得陣列@tags中所有標籤的所有論壇。如何從查詢中獲取缺少的值?

@forums = Forum.joins(:forum_tags).where(:forum_tags => {:name => @tags}) 
       .group("forums.id").having(['COUNT(*) = ?', @tags.length]) 
       .includes(:forum_tags).all 

但是,我也希望能夠找到返回的論壇的一部分,但不是在數組中的標籤。類似於問題頁面右側的Stackoverflow'相關標籤'列表。

舉例來說,如果我有4個論壇:

forum A = tags are ['foo', 'bar'] 
forum B = tags are ['foo', 'bar', 'blah'] 
forum C = tags are ['foo', 'bar', 'blee'] 
forum D = tags are ['blah'] 

if @tags = ['foo','bar'], then it should return ['blah','blee'] 
    with the count on blah=1, and count on blee=1 

回答

0
where("forum_tags.name NOT IN (?)", @tags)