Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何查询关联模型上的对象数?_Ruby On Rails_Activerecord_Ruby On Rails 5_Active Record Query - Fatal编程技术网

Ruby on rails 如何查询关联模型上的对象数?

Ruby on rails 如何查询关联模型上的对象数?,ruby-on-rails,activerecord,ruby-on-rails-5,active-record-query,Ruby On Rails,Activerecord,Ruby On Rails 5,Active Record Query,我有一个档案,而有很多:评级 我要查找的是具有多个关联评级记录的Profile对象的数量 我尝试了以下方法,但无效: > Profile.includes(:ratings).where('ratings.count > 0').count (38.2ms) SELECT COUNT(*) FROM "profiles" WHERE (ratings.count > 0) ActiveRecord::StatementInvalid: PG::UndefinedTabl

我有一个
档案
,而
有很多:评级

我要查找的是具有多个关联评级记录的
Profile
对象的数量

我尝试了以下方法,但无效:

> Profile.includes(:ratings).where('ratings.count > 0').count
   (38.2ms)  SELECT COUNT(*) FROM "profiles" WHERE (ratings.count > 0)
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "ratings"
LINE 1: SELECT COUNT(*) FROM "profiles" WHERE (ratings.count > 0)
                                               ^
: SELECT COUNT(*) FROM "profiles" WHERE (ratings.count > 0)

注意我的
评级
模型不包括名为
计数
的列。我试图做的是
count
计算与每个
profile
记录关联的
ratings
对象的数量,并返回具有多个
1
rating关联对象的
profile
记录的数量

如何使用ActiveRecord实现这一点

编辑1

根据用户793789的建议,再尝试两个查询:

> Profile.includes(:ratings).where('ratings.count > 1').references(:ratings).count
   (55.3ms)  SELECT COUNT(DISTINCT "profiles"."id") FROM "profiles" LEFT OUTER JOIN "ratings" ON "ratings"."profile_id" = "profiles"."id" WHERE (ratings.count > 1)
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  aggregate functions are not allowed in WHERE
LINE 1: ...N "ratings"."profile_id" = "profiles"."id" WHERE (ratings.co...
                                                             ^
: SELECT COUNT(DISTINCT "profiles"."id") FROM "profiles" LEFT OUTER JOIN "ratings" ON "ratings"."profile_id" = "profiles"."id" WHERE (ratings.count > 1)

> Profile.joins(:ratings).where('ratings.count > 1').count
   (40.4ms)  SELECT COUNT(*) FROM "profiles" INNER JOIN "ratings" ON "ratings"."profile_id" = "profiles"."id" WHERE (ratings.count > 1)
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  aggregate functions are not allowed in WHERE
LINE 1: ...N "ratings"."profile_id" = "profiles"."id" WHERE (ratings.co...
                                                             ^
: SELECT COUNT(*) FROM "profiles" INNER JOIN "ratings" ON "ratings"."profile_id" = "profiles"."id" WHERE (ratings.count > 1)
OP的编辑1

经过多方支持和努力,我们最终决定:

Profile.joins(:ratings).group('profiles.id').having('count(ratings.id) > 0').length
但我觉得必须有一个更简单的方法来做到这一点

OP的编辑1

经过多方支持和努力,我们最终决定:

Profile.joins(:ratings).group('profiles.id').having('count(ratings.id) > 0').length

但我觉得必须有一个更简单的方法来做到这一点。

或Profile.joins(:ratings)。where('ratings.count>0')。countNope……这两种方法都不适合我。他们为你工作了吗?
档案有很多:评分
。刷新问题,我发布了两个建议的错误日志。我认为postgres将ratings.count误认为是一个函数。也许你可以尝试在你的表中重命名count列哦…我的评分表中没有
count
列。我指的是rails方法
.count
。我要做的是检索每个
profile
对象上的评级数量,然后只返回具有多个关联
rating
对象的
profiles
的数量。我希望这更清楚。或Profile.joins(:ratings)。where('ratings.count>0')。countNope…这两个都不适合我。他们为你工作了吗?
档案有很多:评分
。刷新问题,我发布了两个建议的错误日志。我认为postgres将ratings.count误认为是一个函数。也许你可以尝试在你的表中重命名count列哦…我的评分表中没有
count
列。我指的是rails方法
.count
。我要做的是检索每个
profile
对象上的评级数量,然后只返回具有多个关联
rating
对象的
profiles
的数量。我希望这更清楚。
Profile.joins(:ratings).group('profiles.id').having('count(ratings.id) > 0').length