Ich versuche, PG_SEARCH zu verwenden, um ein zugehöriges Modell zu durchsuchen. Wenn ich eine Suche ausführe, erhalte ich den Fehler "PG :: Fehler: Fehler: Spaltenpläne.Name existiert nicht". Ich führe die Suche im Modell "Pläne" aus und versuche, mit einer Spalte "Name" gegen die Assoziation zu suchen. Das Has_Many: Durch das Modell, das diese verbindet, ist polymorph. Irgendwie kombiniert die SQL -Abfrage die beiden und wirft einen Fehler. Ich habe die Associated_against Migration (Rails G PG_SEARCH: Migration: Associated_Against) ausgeführt, die Dokumentation durchsucht und nach anderen mit dem Fehler gesucht und nichts erfunden. Es muss sein, dass ich nur etwas übersehen werde. Es wird korrekt ausgeführt (ohne die umfangreicheren Suchergebnisse), wenn ich nur die adociated_against -Linie in Plan.RB entfernen. Jede Hilfe wäre geschätzt!
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
Regler:
def index
query = params[:query]
@plans = Plan.text_query(query)
end
Vollständige Fehlermeldung:
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