Sto cercando di usare PG_Search per cercare attraverso un modello associato. Quando eseguo una ricerca ricevo l'errore "PG :: ERRORE: ERRORE: PIANI DI PIGNO. NAME NON ESPERE". Sto eseguendo la ricerca nel modello "Piani" e sto cercando di cercare contro l'associazione il "luogo" con un "nome" della colonna. Has_Many: attraverso il modello che collega questi è polimorfico. In qualche modo la query SQL sta combinando i due e lancia un errore. Ho eseguito la migrazione associata_against (Rails G PG_Search: Migration: Associated_Against), ho setacciato la documentazione e ho cercato gli altri con l'errore e non trovare nulla, deve essere che sto solo trascurando qualcosa. Funziona correttamente (senza i risultati di ricerca più estesi) se rimuovo la linea associata_against in plan.rb. Qualsiasi aiuto sarebbe apprezzato!
Plan.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
Messaggio di errore completo:
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