fix: search with status and author (#5152)

This commit is contained in:
Tejaswini Chile
2022-08-02 15:28:27 +05:30
committed by GitHub
parent d9b102cff0
commit 596b611fc0
4 changed files with 24 additions and 5 deletions

View File

@@ -58,6 +58,8 @@ class Article < ApplicationRecord
scope :search_by_category_slug, ->(category_slug) { where(categories: { slug: category_slug }) if category_slug.present? }
scope :search_by_category_locale, ->(locale) { where(categories: { locale: locale }) if locale.present? }
scope :search_by_author, ->(author_id) { where(author_id: author_id) if author_id.present? }
scope :search_by_status, ->(status) { where(status: status) if status.present? }
# TODO: if text search slows down https://www.postgresql.org/docs/current/textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS
pg_search_scope(
@@ -77,7 +79,10 @@ class Article < ApplicationRecord
def self.search(params)
records = joins(
:category
).search_by_category_slug(params[:category_slug]).search_by_category_locale(params[:locale])
).search_by_category_slug(
params[:category_slug]
).search_by_category_locale(params[:locale]).search_by_author(params[:author_id]).search_by_status(params[:status])
records = records.text_search(params[:query]) if params[:query].present?
records.page(current_page(params))
end