Associated_against를 사용하는 pg_search에서 "열 [model_name].[associated_column_name]이 존재하지 않습니다"라는 오류가 발생합니다.

StackOverflow https://stackoverflow.com/questions/19855477

문제

pg_search를 사용하여 관련 모델을 검색하려고 합니다.검색을 실행하면 "PG::Error:" 오류가 발생합니다.오류:열 plan.name이 존재하지 않습니다.""계획" 모델에서 검색을 실행 중이며 "장소"와 "이름" 열의 연결을 검색하려고 합니다.이들을 연결하는 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

장소.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

제어 장치:

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top