2010-10-05 43 views
0

我在文章和类别:M关系:轨道:凡是有过滤器类别至少一个文章

我找的分类模型中发现语句,这样我可以得到所有类别的女巫至少包括一篇文章。

应该很容易,但我没有找到一个有效的解决方案,没有搜索检索所有文章。

感谢, Maechi

回答

1

我认为计数器缓存这里是你的朋友。看看here

您可以在柜台缓存添加到categories表和你不喜欢

class CategoryArticles 
    belongs_to :article 
    belongs_to :category, :counter_cache => true 
end 

的CategoryArticles所以,你可以用

@categories = Category.find(:all, :conditions => ["category_articles_count > ?", 0]) 
+0

那正是我需要找到自己的等级!你知道我没有通过实施has_and_belongs_to_many属性来获得类别条款模型吗? – Markus 2010-10-05 10:04:24

+0

我不确定你可以用'has_and_belong_to_many'来做到这一点。我的建议是这样做。 类CategoryArticles belongs_to的:文章 belongs_to的:类别:counter_cache =>真正 结束 类分类01​​的has_many:文章:通过=>:category_articles 的has_many:category_articles 结束 类文章 的has_many:类别, :通过=>:category_articles has_many:category_articles end – Fran 2010-10-05 11:11:21

+1

刚才看到你不能格式化评论中的代码...你可以看看这个要点http://gist.github.com/611736 – Fran 2010-10-05 15:33:03