我在Rails应用程序中使用带有postgresql的smartlisting gem。该表显示了关联结果的元素列表,在某些情况下,该元素可以重复,但是我只需要在表中显示一次该元素,但是当我向活动记录中添加DISTINCT或uniq时,它将转换为数组和智能列表显示和错误。然后,我尝试使用gruop解决问题,并且成功了!但是,当我尝试使用pg_scope进行全文搜索时,pg查询会崩溃。这里的代码:
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
查询
search = User.find(id).prospects.select("prospects.*").group("prospects.id")
search = search.search_query("Say my name") if search_full_text.present?
该小组致力于删除活动记录中的重复项,但是当需要全文搜索时,我会遇到下一个错误
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_...
答案 0 :(得分:0)
经过数小时的搜索,解决此问题的唯一方法是在全文搜索之后进行分组,并在SELECT中的查询中添加一个名为“ rank”的变量,如下所示:
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