From f68124a6dff2b0b6131280be2378a049d9d839d4 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 16 Mar 2023 22:34:51 -0600 Subject: Mejora las queries --- app/controllers/api/companies_controller.rb | 4 ++-- app/controllers/api/products_controller.rb | 11 +++++++++-- app/controllers/serializers/product_serializer.rb | 7 ++++--- 3 files changed, 15 insertions(+), 7 deletions(-) (limited to 'app') diff --git a/app/controllers/api/companies_controller.rb b/app/controllers/api/companies_controller.rb index 086ac5b..973eedb 100644 --- a/app/controllers/api/companies_controller.rb +++ b/app/controllers/api/companies_controller.rb @@ -4,7 +4,7 @@ module Api # CompaniesController class CompaniesController < MasterController def index - @companies = Company.page(params[:page]) + @companies = Company.all render json: serialized_collection.serializable_hash, status: 200 end @@ -44,7 +44,7 @@ module Api end def serialized_collection - Serializers::CompanySerializer.new(@companies.page(params[:page])) + Serializers::CompanySerializer.new(@companies.includes(logo_attachment: :blob).page(params[:page])) end def permitted_params diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index 213fa9f..2822a21 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -49,11 +49,18 @@ module Api private def serialized_object - Serializers::ProductSerializer.new(@product) + Serializers::ProductSerializer.new( + @product.joins(:company) + .select('products.*', 'companies.name as company_name','companies.short_name as company_short_name') + ) end def serialized_collection - Serializers::ProductSerializer.new(@products.page(params[:page])) + Serializers::ProductSerializer.new( + @products.joins(:company) + .select('products.*', 'companies.name as company_name', 'companies.short_name as company_short_name') + .includes(picture_attachment: :blob).page(params[:page]) + ) end def permitted_params diff --git a/app/controllers/serializers/product_serializer.rb b/app/controllers/serializers/product_serializer.rb index 09310a0..8e1ac11 100644 --- a/app/controllers/serializers/product_serializer.rb +++ b/app/controllers/serializers/product_serializer.rb @@ -3,16 +3,17 @@ module Serializers # ProductSerializer class ProductSerializer < BaseSerializer + include Rails.application.routes.url_helpers extend ActionView::RoutingUrlFor - attributes :name, :unitary_price, :bulk_price, :available_quantity, :categories, :company + attributes :name, :unitary_price, :bulk_price, :available_quantity, :categories attribute :picture do |object| object.picture.url end attribute :company do |object| - { name: object.company.name, short_name: object.company.short_name } + { name: object.company_name, short_name: object.company_short_name } end end -end \ No newline at end of file +end -- cgit v1.2.3