From 4d92cafd3fca92f801a90c2c6a384e4d631dff65 Mon Sep 17 00:00:00 2001 From: Shubham Kumar Date: Tue, 31 Jan 2023 17:28:14 +0530 Subject: [PATCH] feat: Order articles by updated_at (#6324) * feat: Order articles by updated_at * Sort before pagination --------- Co-authored-by: Fayaz Ahmed <15716057+fayazara@users.noreply.github.com> Co-authored-by: Vishnu Narayanan Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com> --- app/controllers/api/v1/accounts/articles_controller.rb | 2 +- app/models/article.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/accounts/articles_controller.rb b/app/controllers/api/v1/accounts/articles_controller.rb index a7891ffd4..23855267d 100644 --- a/app/controllers/api/v1/accounts/articles_controller.rb +++ b/app/controllers/api/v1/accounts/articles_controller.rb @@ -8,7 +8,7 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController @portal_articles = @portal.articles @all_articles = @portal_articles.search(list_params) @articles_count = @all_articles.count - @articles = @all_articles.page(@current_page) + @articles = @all_articles.order_by_updated_at.page(@current_page) end def create diff --git a/app/models/article.rb b/app/models/article.rb index 351053380..77b86b2ca 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -59,6 +59,7 @@ class Article < ApplicationRecord 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? } + scope :order_by_updated_at, -> { reorder(updated_at: :desc) } # TODO: if text search slows down https://www.postgresql.org/docs/current/textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS pg_search_scope(