查找具有大于某个关联的x的所有对象 [英] Find all objects that have greater than x of a certain association

查看:48
本文介绍了查找具有大于某个关联的x的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多农场,每个农场都有很多动物。我需要找到每个拥有5种以上动物的农场。

I have many Farms and each farm has many animals. I need to find every farm that has more than 5 animals.

我需要一些与此类似的东西...:

I need something along the lines of this...:

Farm.where(animals.count > 5)  

更新/答案:

Farm.joins(:animals).group("farm_id").having("count(farm_id) > 5")


推荐答案

尝试:

Farm.joins(:animals).group("farm.id").having("count(animals.id) > ?",5)

参考: https://stackoverflow.com/a/9370734/429758

这篇关于查找具有大于某个关联的x的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆