开发者

Rails: List of categories where related items count is greater than 0

Rails model: Categories have items

Trying to get a list of categories that actually have items.

Something like:

@categories = Categor开发者_JS百科y.where(category.items.count > 0).all

Thanks!


Rails 3?

Category.joins(:items).select('distinct categories.*')

should work.


I don't have access to a terminal right now, so I can't test this.. but I think it should work:

Items.find(:all).categories.uniq

This will return any category which is associated with an Item object (so, items.count > 0).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜