From b5fb98dc10ca102661be2697c037713e7eea58e3 Mon Sep 17 00:00:00 2001 From: Kosty Maleyev Date: Thu, 9 Jan 2025 09:57:19 -0800 Subject: [PATCH] [rails] Fix after Postgres upgrade, attempt #4 (#658) * [react, rails] rollback some unnecessary changes in autofix-generate PR #655 * [rails] Revert all previous attemtpts * [rails] fix reviews serialization after Postgres upgrade --- ruby-on-rails/Gemfile | 2 -- .../controllers/api/v1/products_controller.rb | 12 +++++++----- .../app/serializers/product_serializer.rb | 18 ------------------ 3 files changed, 7 insertions(+), 25 deletions(-) delete mode 100644 ruby-on-rails/app/serializers/product_serializer.rb diff --git a/ruby-on-rails/Gemfile b/ruby-on-rails/Gemfile index 44f79bb6..62140c82 100644 --- a/ruby-on-rails/Gemfile +++ b/ruby-on-rails/Gemfile @@ -18,8 +18,6 @@ gem 'nokogiri', '~>1.17.2' # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible gem 'rack-cors' gem 'pg' -# Add serializer for proper JSON handling -gem 'active_model_serializers', '~> 0.10.13' gem "stackprof" gem "sentry-ruby" diff --git a/ruby-on-rails/app/controllers/api/v1/products_controller.rb b/ruby-on-rails/app/controllers/api/v1/products_controller.rb index 69ef7197..4974e272 100644 --- a/ruby-on-rails/app/controllers/api/v1/products_controller.rb +++ b/ruby-on-rails/app/controllers/api/v1/products_controller.rb @@ -12,12 +12,14 @@ def index span_products_db.finish # n+1 to db if done this way - products.each do |prod_slow| + products = products.map do |prod| span_products_slow_db = transaction.start_child(op: "custom.reviews_slow_db_call") - prod_slow["pg_sleep"] = "" - prod_slow["reviews"] = [] - prod_slow["reviews"] = Reviews.select("id, productid, rating, customerid, description, created, Null as pg_sleep").where("productid="+prod_slow.id.to_s) + prod_attrs = prod.attributes + prod_attrs["pg_sleep"] = "" + prod_attrs["reviews"] = [] + prod_attrs["reviews"] = Reviews.select("id, productid, rating, customerid, description, created, Null as pg_sleep").where("productid="+prod.id.to_s).as_json span_products_slow_db.finish + prod_attrs end # fewer db calls this way -- done in products-join @@ -40,7 +42,7 @@ def index # end # span_response.finish - render json: products, each_serializer: ProductSerializer, status: 200 + render json: products, status: 200 end # sent here if unexpected route was enterered diff --git a/ruby-on-rails/app/serializers/product_serializer.rb b/ruby-on-rails/app/serializers/product_serializer.rb deleted file mode 100644 index c068a289..00000000 --- a/ruby-on-rails/app/serializers/product_serializer.rb +++ /dev/null @@ -1,18 +0,0 @@ -class ProductSerializer < ActiveModel::Serializer - attributes :id, :title, :description, :descriptionfull, :price, :img, :imgcropped, :pg_sleep, :reviews - - def reviews - reviews_relation = object.reviews.select("id, productid, rating, customerid, description, created") - reviews_relation.map do |review| - { - id: review.id, - productid: review.productid, - rating: review.rating, - customerid: review.customerid, - description: review.description, - created: review.created, - pg_sleep: nil - } - end - end -end \ No newline at end of file