gpt4 book ai didi

ruby-on-rails - pg_search 使用 associated_against 给出错误 "column [model_name].[associated_column_name] does not exist"

转载 作者:数据小太阳 更新时间:2023-10-29 07:00:37 26 4
gpt4 key购买 nike

我正在尝试使用 pg_search 来搜索关联模型。当我运行搜索时,出现错误“PG::Error: ERROR: column plans.name does not exist”。我正在“计划”模型中运行搜索,并尝试针对“地点”与列“名称”的关联进行搜索。连接这些的 has_many :through 模型是多态的。不知何故,sql 查询将两者结合起来并抛出错误。我已经运行了 associated_against 迁移 (rails g pg_search:migration:associated_against),搜索了文档,并寻找其他有错误的人,但一无所获,一定是我只是忽略了一些东西。如果我只是删除 plan.rb 中的 associated_against 行,它会正确运行(没有更广泛的搜索结果)。任何帮助将不胜感激!

计划.rb:

class Plan < ActiveRecord::Base
belongs_to :user
has_many :plan_places, :dependent => :destroy
has_many :places, through: :plan_places, source: :plan

include PgSearch
pg_search_scope :search, :against => [:title, :summary],
associated_against: { places: [:name, :address]},
using: {tsearch: {dictionary: "english"}},
ignoring: :accents

def self.text_query(query)
if query.present?
search(query)
else
scoped
end
end
end

Place.rb:

class Place < ActiveRecord::Base
has_many :plan_places, as: :sortable #polymorphic -- could this be the issue??
has_many :plans, through: :plan_places

include PgSearch
multisearchable :against => [:name, :address]
pg_search_scope :search, against: [:name, :address],
using: {tsearch: {dictionary: "english"}},
ignoring: :accents
def self.text_query(query)
if query.present?
search(query)
else
scoped
end
end
end

Controller :

def index
query = params[:query]
@plans = Plan.text_query(query)
end

完整错误信息:

PG::Error: ERROR:  column plans.name does not exist
LINE 1: ...OUTER JOIN (SELECT "plans"."id" AS id, string_agg("plans"."n...
^
: SELECT "plans".*, ((ts_rank((to_tsvector('english', unaccent(coalesce("plans"."title"::text, ''))) || to_tsvector('english', unaccent(coalesce("plans"."summary"::text, ''))) || to_tsvector('english', unaccent(coalesce(pg_search_ef8b0c36567cc241900c73.pg_search_1d546fcf34c118d2a7b8f6::text, ''))) || to_tsvector('english', unaccent(coalesce(pg_search_ef8b0c36567cc241900c73.pg_search_f3147101e01c522d780049::text, '')))), (to_tsquery('english', ''' ' || unaccent('giraffe') || ' ''')), 0))) AS pg_search_rank FROM "plans" LEFT OUTER JOIN (SELECT "plans"."id" AS id, string_agg("plans"."name"::text, ' ') AS pg_search_1d546fcf34c118d2a7b8f6, string_agg("plans"."address"::text, ' ') AS pg_search_f3147101e01c522d780049 FROM "plans" INNER JOIN "plan_places" ON "plan_places"."plan_id" = "plans"."id" INNER JOIN "plans" "places_plans" ON "places_plans"."id" = "plan_places"."plan_id" GROUP BY "plans"."id") pg_search_ef8b0c36567cc241900c73 ON pg_search_ef8b0c36567cc241900c73.id = "plans"."id" WHERE (((to_tsvector('english', unaccent(coalesce("plans"."title"::text, ''))) || to_tsvector('english', unaccent(coalesce("plans"."summary"::text, ''))) || to_tsvector('english', unaccent(coalesce(pg_search_ef8b0c36567cc241900c73.pg_search_1d546fcf34c118d2a7b8f6::text, ''))) || to_tsvector('english', unaccent(coalesce(pg_search_ef8b0c36567cc241900c73.pg_search_f3147101e01c522d780049::text, '')))) @@ (to_tsquery('english', ''' ' || unaccent('giraffe') || ' ''')))) ORDER BY pg_search_rank DESC, "plans"."id" ASC, created_at DESC

最佳答案

如果其他人遇到这种情况,我最终使用了一种有点老套的解决方法。在计划模型的搜索方法中,我只是在子模型(地点)上运行搜索,使用 has_many :through 表 (plan_places) 获取结果的关联计划,并将此列表附加到常规计划搜索结果中。它很丑陋而且可能非常低效,所以如果有人知道“真正的”答案,请提交!

def self.text_query(query)
if query.present?
place_ids = Place.text_query(query).map(&:id).uniq
plan_ids = PlanPlace.where(sortable_id: place_ids, sortable_type: "Place").map(&:plan_id).uniq
plans = find(plan_ids)
plans = plans + search(query)
else
scoped
end
end

关于ruby-on-rails - pg_search 使用 associated_against 给出错误 "column [model_name].[associated_column_name] does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19855477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com