I am trying to use pg_search to search on a related model. When I start the search, I get the error message "PG :: Error: ERROR: column plans.name does not exist." I search the βplansβ model and try to search by combining βplaceβ with the column βnameβ. Has_many: through the model that connects them, is polymorphic. Somehow, the sql query combines the two and throws an error. I ran the associated migration_agest (rails g pg_search: migration: associated_against), looked at the documentation and looked for others with an error and didn't come up with anything, I must be just missing something. It works correctly (without more extensive search results) if I just delete the linked link_string in plan.rb. Any help would be appreciated!
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
Full error message:
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