From b0fc081d7998f17997e6a3c415731f8461cd9223 Mon Sep 17 00:00:00 2001 From: lukaszreszke Date: Wed, 25 Sep 2024 13:17:53 +0200 Subject: [PATCH] Start reading stock level from product catalog --- rails_application/app/controllers/products_controller.rb | 8 ++++++-- rails_application/app/models/product.rb | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rails_application/app/controllers/products_controller.rb b/rails_application/app/controllers/products_controller.rb index bcd92726..f6403231 100644 --- a/rails_application/app/controllers/products_controller.rb +++ b/rails_application/app/controllers/products_controller.rb @@ -1,10 +1,14 @@ class ProductsController < ApplicationController def index - @products = Product.all + @products = Product.joins(:product_catalog).select("products.id, products.name, products.price, products.vat_rate, products.sku, product_catalogs.stock_level") end def show - @product = Product.find(params[:id]) + @product = Product + .where(id: params[:id]) + .joins(:product_catalog) + .select("products.id, products.name, products.price, products.vat_rate, products.sku, product_catalogs.stock_level") + .first end def new diff --git a/rails_application/app/models/product.rb b/rails_application/app/models/product.rb index 787c5b67..ada3829b 100644 --- a/rails_application/app/models/product.rb +++ b/rails_application/app/models/product.rb @@ -8,6 +8,8 @@ class Product < ApplicationRecord validate :validate_vat_rate validates :sku, presence: true + has_one :product_catalog, class_name: "Inventory::ProductCatalog", foreign_key: :product_id + default_scope { where(latest: true) } before_create :set_stock_level