简体   繁体   中英

PostgreSQL error when grouping and search with full text search in SmartListing Gem

I'm using smartlisting gem with postgresql in a rails app. The table shows a list of elements result of an association that in some cases the elements can repeat, but I need to show that element only once in the table, but when I add a DISTINCT or uniq to the active record it transforms in an array and smartlisting shows and error. Then I tried to solve it using a gruop, and IT WORKS! but when I tried to do a fulltext search using pg_scope, the pg query crash. Here the code:

class Prospect < ActiveRecord::Base
  include PgSearch
  pg_search_scope :search_by_name,
                  against: [:name, :father_last_name, :mother_last_name],
                  associated_against: { user: [:first_name, :father_last_name,
                                               :mother_last_name] },
                  ignoring: :accents

  has_many :projects, through: :project_prospects, dependent: :destroy
  .
  .
  .
class User < ActiveRecord::Base
  has_many :project_prospect
  has_many :projects, through: :user_projects, dependent: :nullify
end

The query

search = User.find(id).prospects.select("prospects.*").group("prospects.id")
search = search.search_query("Say my name") if search_full_text.present? 

The group works to delete duplicates in the activerecord but when the full text search is needed I get the next error

PG::GroupingError: ERROR:  column "pg_search_ece0055f6ad1de91cd168e.rank" must appear in 
the GROUP BY clause or be used in an aggregate function 

LINE 1: ...168e.pg_search_id GROUP BY prospects.id  ORDER BY pg_search_...

After hours searching the only solution for this problem was make the group after the full text search and add a variable named 'rank' to the query in the SELECT, like this:

search = User.find(id).prospects
if search_full_text.present? 
    search = search.search_query("Say my name")
                   .select("prospects.*").group("prospects.id, rank")
else
    search = search.select("prospects.*").group("prospects.id")
end

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