Rails 4.1.1 w/ pg_search - “PG::SyntaxError: ERROR

2019-03-21 02:57发布

问题:

I updated from RoR 4.0.4 to 4.1.1 to apply the latest security patch and it appears pg_search broke.

Here's a error:

PG::SyntaxError: ERROR: syntax error at or near "AS" LINE 1: ...sh', ''' ' || unaccent('banner') || ' ''')), 0))) AS pg_sear...

I'm searching via ajax but i don't think that is the issue based on the output above. I'm also using will_paginate but the branch I'm using is supposed to fix previous issues with rails 4.1.x and pg_search.

gem 'will_paginate', :git => 'https://github.com/nazgum/will_paginate.git' #until rails 4.1 will_paginate fixed

This is the code model where i'm executing the search:

  include PgSearch
  pg_search_scope :search, against: [:title, :body],
  using: {tsearch: {dictionary: "english"}},
  ignoring: :accents

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

Everything was working prior to the RoR version bump - hoping someone might have some advice.

Here's the full stacktrace:

: SELECT COUNT("photos".*, ((ts_rank((to_tsvector('english', unaccent(coalesce("photos"."description"::text, ''))) || to_tsvector('english', unaccent(coalesce(pg_search_3b58f11462e6f0086ceffb.pg_search_dc60169d7766a303bd09de::text, '')))), (to_tsquery('english', ''' ' || unaccent('"banner"') || ' ''')), 0))) AS pg_search_rank) FROM "photos" LEFT OUTER JOIN (SELECT "photos"."id" AS id, string_agg("products"."name"::text, ' ') AS pg_search_dc60169d7766a303bd09de FROM "photos" INNER JOIN "products" ON "products"."id" = "photos"."product_id" GROUP BY "photos"."id") pg_search_3b58f11462e6f0086ceffb ON pg_search_3b58f11462e6f0086ceffb.id = "photos"."id" WHERE "photos"."placement" = 'showcase' AND (((to_tsvector('english', unaccent(coalesce("photos"."description"::text, ''))) || to_tsvector('english', unaccent(coalesce(pg_search_3b58f11462e6f0086ceffb.pg_search_dc60169d7766a303bd09de::text, '')))) @@ (to_tsquery('english', ''' ' || unaccent('"banner"') || ' ''')))) AND "photos"."is_active" = 't':
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract_adapter.rb:362:in `translate_exception_class'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract_adapter.rb:375:in `rescue in log'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract_adapter.rb:373:in `log'
  activerecord (4.1.1) lib/active_record/connection_adapters/postgresql_adapter.rb:815:in `exec_no_cache'
  activerecord (4.1.1) lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
  activerecord (4.1.1) lib/active_record/connection_adapters/postgresql_adapter.rb:947:in `select'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:31:in `select_all'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract/query_cache.rb:67:in `block in select_all'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract/query_cache.rb:82:in `cache_sql'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract/query_cache.rb:67:in `select_all'
  activerecord (4.1.1) lib/active_record/relation/calculations.rb:254:in `execute_simple_calculation'
  activerecord (4.1.1) lib/active_record/relation/calculations.rb:216:in `perform_calculation'
  activerecord (4.1.1) lib/active_record/relation/calculations.rb:111:in `calculate'
  activerecord (4.1.1) lib/active_record/relation/calculations.rb:26:in `count'
  app/views/showcases/_index.html.haml:1:in `_app_views_showcases__index_html_haml__780171647705973812_70320535206900'
  actionview (4.1.1) lib/action_view/template.rb:145:in `block in render'
  activesupport (4.1.1) lib/active_support/notifications.rb:161:in `instrument'
  actionview (4.1.1) lib/action_view/template.rb:339:in `instrument'
  actionview (4.1.1) lib/action_view/template.rb:143:in `render'
  actionview (4.1.1) lib/action_view/renderer/partial_renderer.rb:306:in `render_partial'
  actionview (4.1.1) lib/action_view/renderer/partial_renderer.rb:279:in `block in render'
  actionview (4.1.1) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
  actionview (4.1.1) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
  actionview (4.1.1) lib/action_view/renderer/partial_renderer.rb:278:in `render'
  actionview (4.1.1) lib/action_view/renderer/renderer.rb:47:in `render_partial'
  actionview (4.1.1) lib/action_view/renderer/renderer.rb:21:in `render'
  actionview (4.1.1) lib/action_view/helpers/rendering_helper.rb:32:in `render'
  haml (4.0.5) lib/haml/helpers/action_view_mods.rb:12:in `render_with_haml'
  app/views/showcases/index.js.erb:4:in `_app_views_showcases_index_js_erb__1635704546300671617_70320482208180'
  actionview (4.1.1) lib/action_view/template.rb:145:in `block in render'
  activesupport (4.1.1) lib/active_support/notifications.rb:161:in `instrument'
  actionview (4.1.1) lib/action_view/template.rb:339:in `instrument'
  actionview (4.1.1) lib/action_view/template.rb:143:in `render'
  actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
  actionview (4.1.1) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
  actionview (4.1.1) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
  actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
  actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
  actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
  actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:17:in `render'
  actionview (4.1.1) lib/action_view/renderer/renderer.rb:42:in `render_template'
  actionview (4.1.1) lib/action_view/renderer/renderer.rb:23:in `render'
  actionview (4.1.1) lib/action_view/rendering.rb:99:in `_render_template'
  actionpack (4.1.1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
  actionview (4.1.1) lib/action_view/rendering.rb:82:in `render_to_body'
  actionpack (4.1.1) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
  actionpack (4.1.1) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
  actionpack (4.1.1) lib/abstract_controller/rendering.rb:25:in `render'
  actionpack (4.1.1) lib/action_controller/metal/rendering.rb:16:in `render'
  actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
  activesupport (4.1.1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
  /Users/bret/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
  activesupport (4.1.1) lib/active_support/core_ext/benchmark.rb:12:in `ms'
  actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
  actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
  activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:40:in `render'
  actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
  actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
  actionpack (4.1.1) lib/abstract_controller/base.rb:189:in `process_action'
  actionpack (4.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.1.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `run_callbacks'
  actionpack (4.1.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.1.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.1.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.1.1) lib/abstract_controller/base.rb:136:in `process'
  actionview (4.1.1) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.1.1) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.1.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.1.1) lib/action_controller/metal.rb:231:in `block in action'
  actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
  actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
  actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676:in `call'
  omniauth (1.2.1) lib/omniauth/strategy.rb:404:in `call_app!'
  omniauth-identity (1.1.1) lib/omniauth/strategies/identity.rb:43:in `other_phase'
  omniauth (1.2.1) lib/omniauth/strategy.rb:185:in `call!'
  omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.1) lib/omniauth/builder.rb:59:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/flash.rb:254:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.1.1) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
  actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  sentry-raven (0.8.0) lib/raven/rack.rb:47:in `call'
  railties (4.1.1) lib/rails/engine.rb:514:in `call'
  railties (4.1.1) lib/rails/application.rb:144:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  railties (4.1.1) lib/rails/rack/debugger.rb:20:in `call'
  thin (1.6.2) lib/thin/connection.rb:86:in `block in pre_process'
  thin (1.6.2) lib/thin/connection.rb:84:in `pre_process'
  thin (1.6.2) lib/thin/connection.rb:53:in `process'
  thin (1.6.2) lib/thin/connection.rb:39:in `receive_data'
  eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
  thin (1.6.2) lib/thin/backends/base.rb:73:in `start'
  thin (1.6.2) lib/thin/server.rb:162:in `start'
  rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
  rack (1.5.2) lib/rack/server.rb:264:in `start'
  railties (4.1.1) lib/rails/commands/server.rb:69:in `start'
  railties (4.1.1) lib/rails/commands/commands_tasks.rb:81:in `block in server'
  railties (4.1.1) lib/rails/commands/commands_tasks.rb:76:in `server'
  railties (4.1.1) lib/rails/commands/commands_tasks.rb:40:in `run_command!'
  railties (4.1.1) lib/rails/commands.rb:17:in `<top (required)>'
  bin/rails:4:in `<main>'

回答1:

@supports.count(:all) should work, the @supports.count syntax got deprecated in rails 4.1.1



回答2:

There is a good conversation here to clarify the problem.



回答3:

This is weird - when i call

=@supports.count

the error is generated but it is a valid object. It's a bit hacky but this is working for now:

=@supports.to_a.count