Skip to content

trying to fetch ids of a pg_search_scope while also doing an includes raises "PG::GroupingError" #553

@timdiggins

Description

@timdiggins

Given a Entry model that has a relation (I've tested with has_one, belongs_to, has_one through ) called project
and a pg_search_scope :search_by_text,

then trying to get the ids while also including the project association

Entry.includes(:project).search_by_text("unexpurgated").ids

raises an error similar to #238

ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  column "pg_search_87d05cd08817497883161d.rank" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...61d.pg_search_id GROUP BY "entries"."id" ORDER BY pg_search_...

Workaround is to .reorder('') before calling .ids, ie.

Entry.includes(:project).search_by_text("unexpurgated").reorder("").ids

Note that all of these are fine:

Entry.includes(:project).search_by_text("unexpurgated").pluck(:id)
Entry.search_by_text("unexpurgated").ids

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions