From ef2ce98e464e14be910e0abf14fcde1bef453e1f Mon Sep 17 00:00:00 2001 From: benni Date: Tue, 31 Jul 2012 17:37:32 +0200 Subject: [PATCH] Performance improvement for stockit view. --- app/controllers/stockit_controller.rb | 2 +- app/models/stock_article.rb | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index 40f790e8..231bc35e 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -1,7 +1,7 @@ class StockitController < ApplicationController def index - @stock_articles = StockArticle.joins(:supplier, :article_category). + @stock_articles = StockArticle.includes(:supplier, :article_category). order('suppliers.name, article_categories.name, articles.name') end diff --git a/app/models/stock_article.rb b/app/models/stock_article.rb index 59f29238..6050c8c4 100644 --- a/app/models/stock_article.rb +++ b/app/models/stock_article.rb @@ -14,15 +14,9 @@ class StockArticle < Article end # Check for unclosed orders and substract its ordered quantity - def quantity_available(exclude_order = nil) - available = quantity - for order in Order.stockit.all(:conditions => "state = 'open' OR state = 'finished'") - unless order == exclude_order - order_article = order.order_articles.first(:conditions => {:article_id => id}) - available -= order_article.units_to_order if order_article - end - end - available + def quantity_available + quantity - OrderArticle.where(article_id: id). + joins(:order).where("orders.state = 'open' OR orders.state = 'finished'").sum(:units_to_order) end def self.stock_value