开发者

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

开发者 https://www.devze.com 2023-02-19 06:22 出处:网络
Rails model: Categories have items Trying to get a list of categories that actually have items. Something like:

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

精彩评论

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