简体   繁体   中英

Rails postgres query - Model.where(model.has_many_relationship_item.count >= 2)?

I need to return an array of a category where a particular item of which it has_many , has a .count >= 2

I know this sytax is wrong, but I'm trying to figure out the correct way, any tips??

Model.where(model.has_many_relationship_item.count >= 2) ??

Thanks in advance!!

The only way I know how to do this is:

Model.joins(:items).group('models.id').having('count(items.id) >= 2')

assuming (for example) the class of the related model is Item

Category.joins(:items).group(“cateogry.id”)。having(“COUNT(items.id)> = 2”)

Model.joins(:association).group('model_table.id').having('COUNT(association_table.id) >= 2')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM