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
).
精彩评论