我的用户模型对follower
关联具有以下定义:
has_many :passive_follow_actions, -> { where("actions.activity_verb = 'follow'").uniq }, class_name: 'Action', foreign_key: :activity_object_id
has_many :followers, through: :passive_follow_actions, source: :activity_actor, source_type: 'User'
#
用户表中有一个名为follow_stats的jsonb字段,可用于对关注者结果进行排序。
单独执行一项操作
has_many :followers, -> { distinct }, through: :passive_follow_actions, source: :activity_actor, source_type: 'User'
# SELECT DISTINCT "users".* FROM "users" INNER JOIN "actions" ON "users"."id" = "actions"."activity_actor_id" WHERE "actions"."activity_object_id" = 1 AND (actions.activity_verb = 'follow') AND "actions"."activity_actor_type" = 'User'
#
订单操作本身也可以
has_many :followers, -> { order("follow_stats->'followers_count' DESC") }, through: :passive_follow_actions, source: :activity_actor, source_type: 'User'
# SELECT "users".* FROM "users" INNER JOIN "actions" ON "users"."id" = "actions"."activity_actor_id" WHERE "actions"."activity_object_id" = 1 AND (actions.activity_verb = 'follow') AND "actions"."activity_actor_type" = 'User' ORDER BY follow_stats->'followers_count' DESC
#
但是,放在一起,
has_many :followers, -> { distinct.order("follow_stats->'followers_count' DESC") }, through: :passive_follow_actions, source: :activity_actor, source_type: 'User'
# SELECT DISTINCT "users".* FROM "users" INNER JOIN "actions" ON "users"."id" = "actions"."activity_actor_id" WHERE "actions"."activity_object_id" = 1 AND (actions.activity_verb = 'follow') AND "actions"."activity_actor_type" = 'User' ORDER BY follow_stats->'followers_count' DESC
#
它引发以下异常:
PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
#
我尝试将jsonb查询添加到select子句中以防止发生错误,但这会导致响应为空:
has_many :followers, -> { select("follow_stats->'followers_count'").uniq.order("follow_stats->'followers_count' DESC") }, through: :passive_follow_actions, source: :activity_actor, source_type: 'User'
# SELECT DISTINCT follow_stats->'followers_count' FROM "users" INNER JOIN "actions" ON "users"."id" = "actions"."activity_actor_id" WHERE "actions"."activity_object_id" = 1 AND (actions.activity_verb = 'follow') AND "actions"."activity_actor_type" = 'User' ORDER BY follow_stats->'followers_count' DESC
#
响应:
[#<User id: nil, ?column?: 42>, #<User id: nil, ?column?: 4>, #<User id: nil, ?column?: 2>, #<User id: nil, ?column?: 1>, #<User id: nil, ?column?: 0>]
#
当我在select子句中添加*时,它将返回相同的异常PG :: InvalidColumnReference
has_many :followers, -> { select("*, follow_stats->'followers_count'").uniq.order("follow_stats->'followers_count' DESC") }, through: :passive_follow_actions, source: :activity_actor, source_type: 'User'
#SELECT DISTINCT *, follow_stats->'followers_count' FROM \"users\" INNER JOIN \"actions\" ON \"users\".\"id\" = \"actions\".\"activity_actor_id\" WHERE \"actions\".\"activity_object_id\" = 1 AND (actions.activity_verb = 'follow') AND \"actions\".\"activity_actor_type\" = 'User' ORDER BY follow_stats->'followers_count' DESC
#
如何通过jsonb键同时选择区分和排序?