Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save crova/0e4cbe40f91961c94b2401f2e3fb3794 to your computer and use it in GitHub Desktop.
Save crova/0e4cbe40f91961c94b2401f2e3fb3794 to your computer and use it in GitHub Desktop.
Getting undefined column with PG_Search + Filterrific query
PG::UndefinedColumn: ERROR: column db_campaigns.partner, does not exist LINE 1: ...(ts_rank((to_tsvector('simple', unaccent(coalesce("db_campai... ^ HINT: Perhaps you meant to reference the column "db_campaigns.partner" or the column "db_campaigns.partner". : SELECT SUM("db_campaigns"."sent") FROM "db_campaigns" INNER JOIN (SELECT "db_campaigns"."id" AS pg_search_id, (ts_rank((to_tsvector('simple', unaccent(coalesce("db_campaigns"."partner,"::text, ''))) || to_tsvector('simple', unaccent(coalesce("db_campaigns"."name,"::text, ''))) || to_tsvector('simple', unaccent(coalesce("db_campaigns"."segment,"::text, ''))) || to_tsvector('simple', unaccent(coalesce("db_campaigns"."theme,"::text, ''))) || to_tsvector('simple', unaccent(coalesce("db_campaigns"."database"::text, '')))), (to_tsquery('simple', ''' ' || unaccent('residencial') || ' ''')), 0)) AS rank FROM "db_campaigns" WHERE (((to_tsvector('simple', unaccent(coalesce("db_campaigns"."partner,"::text, ''))) || to_tsvector('simple', unaccent(coalesce("db_campaigns"."name,"::text, ''))) || to_tsvector('simple', unaccent(coalesce("db_campaigns"."segment,"::text, ''))) || to_tsvector('simple', unaccent(coalesce("db_campaigns"."theme,"::text, ''))) || to_tsvector('simple', unaccent(coalesce("db_campaigns"."database"::text, '')))) @@ (to_tsquery('simple', ''' ' || unaccent('residencial') || ' '''))))) AS pg_search_a98d6606b272f8e1286023 ON "db_campaigns"."id" = pg_search_a98d6606b272f8e1286023.pg_search_id WHERE "db_campaigns"."deleted_at" IS NULL AND (extract(year from scheduled_date) = 2018) LIMIT $1 OFFSET $2
def index
# Set Scenario for the Dashboard
# Fetchs data to Detailed view of each Database
def kpi(campaigns)
{
sent: campaigns.sum(:sent),
orate: (campaigns.sum(:unique_views).to_f / campaigns.sum(:sent).to_f) * 100,
crate: (campaigns.sum(:clicker).to_f / campaigns.sum(:sent).to_f) * 100,
result: (campaigns.sum(:result).to_f),
turnover: campaigns.sum(:turnover),
cost: campaigns.sum(:cost),
margin: campaigns.sum(:margin),
unique_views: campaigns.sum(:unique_views),
clicker: campaigns.sum(:clicker),
ecpm: (campaigns.sum(:turnover).to_f / campaigns.sum(:sent).to_f) * 1000
}
end
@filterrific = initialize_filterrific(
DbCampaign,
params[:filterrific]
) or return
respond_to do |format|
format.html
format.js
end
# Gets campaigns for 2018 (after merge - ABD Only)
@db_campaigns = @filterrific.find.paginate(page: params[:page], per_page: 10).page(params[:page]).camp_2018
@db_campaigns_kpi = kpi(@db_campaigns)
@top_3_margin = DbCampaign.top_margin.three.camp_2018
@latest_3 = DbCampaign.newest_camps.three.camp_2018
# Gets campaigns for 2017 (before merge - ABD Legacy & ABD 2K18)
# ABD LEGACY #
if current_user.admin?
@db_campaigns_abd_2017 = @filterrific.find.paginate(page: params[:page], per_page: 10).page(params[:page]).camp_2017
@db_campaigns_abd_2017_kpi = kpi(@db_campaigns_abd_2017)
else
# ABD 2K18 #
@db_campaigns_abd_2017 = @filterrific.find.paginate(page: params[:page], per_page: 10).page(params[:page]).camp_2017.camp_abd_2k18
@db_campaigns_abd_2017_kpi = kpi(@db_campaigns_abd_2017)
end
end
#scopes
filterrific(
default_filter_params: { sorted_by: 'scheduled_date_desc' },
available_filters: [
:search_query,
:camp_2017,
:camp_2018,
:camp_abd,
:camp_abd_2k18,
:sorted_by,
:search_for
]
)
scope :search_query, lambda { |query| search_by_keywords(query) }
pg_search_scope :search_by_keywords,
against: %i[
partner,
name,
segment,
theme,
database
],
using: {
tsearch: { any_word: true}
},
ignoring: :accents
scope :camp_2017, lambda { where('extract(year from scheduled_date) = ?', 2017)}
scope :camp_2018, lambda { where('extract(year from scheduled_date) = ?', 2018)}
scope :camp_abd, lambda { where("db_campaigns.database like ?", "ABD")}
scope :camp_abd_2k18, lambda { where("db_campaigns.database like ?", "ABD 2K18")}
activerecord (5.0.6) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `async_exec'
activerecord (5.0.6) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `block in exec_no_cache'
activerecord (5.0.6) lib/active_record/connection_adapters/abstract_adapter.rb:590:in `block in log'
activesupport (5.0.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activerecord (5.0.6) lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
activerecord (5.0.6) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `exec_no_cache'
activerecord (5.0.6) lib/active_record/connection_adapters/postgresql_adapter.rb:587:in `execute_and_clear'
activerecord (5.0.6) lib/active_record/connection_adapters/postgresql/database_statements.rb:103:in `exec_query'
activerecord (5.0.6) lib/active_record/connection_adapters/abstract/database_statements.rb:373:in `select'
activerecord (5.0.6) lib/active_record/connection_adapters/abstract/database_statements.rb:41:in `select_all'
activerecord (5.0.6) lib/active_record/connection_adapters/abstract/query_cache.rb:93:in `block in select_all'
activerecord (5.0.6) lib/active_record/connection_adapters/abstract/query_cache.rb:108:in `cache_sql'
activerecord (5.0.6) lib/active_record/connection_adapters/abstract/query_cache.rb:93:in `select_all'
activerecord (5.0.6) lib/active_record/relation/calculations.rb:252:in `execute_simple_calculation'
activerecord (5.0.6) lib/active_record/relation/calculations.rb:207:in `perform_calculation'
activerecord (5.0.6) lib/active_record/relation/calculations.rb:121:in `calculate'
activerecord (5.0.6) lib/active_record/relation/calculations.rb:76:in `sum'
app/controllers/db_campaigns_controller.rb:74:in `kpi'
app/controllers/db_campaigns_controller.rb:99:in `index'
actionpack (5.0.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.6) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.6) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.6) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.6) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.6) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.6) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.6) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.6) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.6) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.6) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.6) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.6) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.6) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.6) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.6) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.6) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.6) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.6) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.6) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.6) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.6) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.6) lib/action_dispatch/routing/route_set.rb:727:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (2.0.3) lib/rack/etag.rb:25:in `call'
rack (2.0.3) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.3) lib/rack/head.rb:12:in `call'
rack (2.0.3) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.3) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.0.6) lib/active_record/migration.rb:553:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.6) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.6) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.6) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.6) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.6) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.6) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.6) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.6) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.6) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.6) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
railties (5.0.6) lib/rails/engine.rb:522:in `call'
puma (3.11.0) lib/puma/configuration.rb:225:in `call'
puma (3.11.0) lib/puma/server.rb:624:in `handle_request'
puma (3.11.0) lib/puma/server.rb:438:in `process_client'
puma (3.11.0) lib/puma/server.rb:302:in `block in run'
puma (3.11.0) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
scope :search_query, lambda { |query|
return nil if query.blank?
terms = query.downcase.split(/\s+/)
terms = terms.map { |e|
(e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
num_or_conds = 5
where(
terms.map { |term|
"(LOWER(db_campaigns.partner) LIKE ? OR LOWER(db_campaigns.name) LIKE ? OR LOWER(db_campaigns.theme) LIKE ? OR LOWER(db_campaigns.segment) LIKE ?OR LOWER(db_campaigns.database) LIKE ?)"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conds }.flatten
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment