2011-08-17 14 views
1

我有以下的Rails型号:Meta_Search:搜索的相关条目的数量

class Entry < ActiveRecord::Base 
    has_and_belongs_to_many :tags, :uniq => true 
end 

而且

class Tag < ActiveRecord::Base 
    has_and_belongs_to_many :entries, :uniq => true 
end 

正是这样很明显,一个“入门”可以有很多“标签”关联它。

使用插件Meta_Search我希望能够执行一个搜索(通过一个表单),该搜索返回具有多于0个与其关联的标签的'条目'。

我已经尝试过几种技术,包括(命名)范围和方法,但我一直未能实现这一点。

有没有人有关于如何执行此操作的想法?

谢谢。

回答

0

喜欢的东西

Entry.joins(
     :tags 
    ).select(
     "entries.*, count(tags.id) as tags_count" 
    ).order(
     "tags_count DESC" 
    ).group( 
     "entries.id" 
    ).where(
     "tags_count != 0" 
    )