spree 3.0仅显示有库存的产品

时间:2015-07-09 14:26:21

标签: ruby-on-rails spree

所以我之前已经问过这个问题,但即使问题得到解答,答案也没有发布,而且由于我没有足够的分数来评论答案,我需要再次提问。这是原帖

Is there a scope to allow me to show only products with variants with stock?

这适用于狂欢3.0。当我添加显示的代码

joins(
  variants_including_master: :stock_items
).group('spree_products.id').having("SUM(count_on_hand) > 0")

像我这样的产品装饰

def self.available(available_on = nil, currency = nil)
    # joins(:master => :prices).where("#{Product.quoted_table_name}.available_on <= ?", available_on || Time.now)      
    joins(:master => :prices).where("#{Product.quoted_table_name}.available_on <= ?", available_on || Time.now).joins(variants_including_master: :stock_items).group('spree_products.id').having("SUM(count_on_hand) > 0")  
end
search_scopes << :available

这打破了视图,因为据我所知,由于GROUP BY,结果有一些奇怪的属性。这个错误出现在<_ p>行的products_helper.rb文件中

  max_updated_at = (@products.maximum(:updated_at) || Date.today).to_s(:number)

因为@ products.maximum(:updated_at)返回哈希

{[6, 6]=>2015-07-04 19:08:36 UTC, [3, 3]=>2015-07-04 19:07:37 UTC, [10, 10]=>2015-07-07 19:01:12 UTC}

而不是返回日期

2015-07-07 19:01:12 UTC

它正常。

此代码也会在分类页面上爆炸,并出现以下错误:

PG :: GroupingError:ERROR:column&#34; spree_products_taxons.position&#34;必须出现在GROUP BY子句中或用于聚合函数 第1行:选择DISTINCT&#34; spree_products&#34;。&#34; id&#34;,spree_products_taxon ...                                                 ^

:SELECT DISTINCT&#34; spree_products&#34;。&#34; id&#34;,spree_products_taxons.position AS alias_0 FROM&#34; spree_products&#34; INNER JOIN&#34; spree_variants&#34; ON&#34; spree_variants&#34;。&#34; product_id&#34; =&#34; spree_products&#34;。&#34; id&#34; AND&#34; spree_variants&#34;。&#34; is_master&#34; =&#39; t&#39; AND&#34; spree_variants&#34;。&#34; deleted_at&#34; IS NULL INNER JOIN&#34; spree_prices&#34; ON&#34; spree_prices&#34;。&#34; variant_id&#34; =&#34; spree_variants&#34;。&#34; id&#34; AND&#34; spree_prices&#34;。&#34; deleted_at&#34; IS NULL INNER JOIN&#34; spree_variants&#34; &#34; variants_including_masters_spree_products&#34; ON&#34; variants_including_masters_spree_products&#34;。&#34; product_id&#34; =&#34; spree_products&#34;。&#34; id&#34; AND&#34; variants_including_masters_spree_products&#34;。&#34; deleted_at&#34; IS NULL INNER JOIN&#34; spree_stock_items&#34; ON&#34; spree_stock_items&#34;。&#34; variant_id&#34; =&#34; variants_including_masters_spree_products&#34;。&#34; id&#34; AND&#34; spree_stock_items&#34;。&#34; deleted_at&#34; IS NULL LEFT OUTER JOIN&#34; spree_products_taxons&#34; ON&#34; spree_products_taxons&#34;。&#34; product_id&#34; =&#34; spree_products&#34;。&#34; id&#34;在哪里&#34; spree_products&#34;。&#34; deleted_at&#34; IS NULL和(&#34; spree_products&#34; .deleted_at IS NULL或&#34; spree_products&#34; .deleted_at&gt; =&#39; 2015-07-09 14:30:53.668422&#39;)AND (&#34; spree_products&#34; .available_on&lt; =&#39; 2015-07-09 14:30:53.669089&#39;)和&#34; spree_products_taxons&#34;。&#34; taxon_id&#34 ; IN(2,7,14)AND(spree_prices.amount IS NOT NULL)和&#34; spree_prices&#34;。&#34;货币&#34; =&#39; USD&#39; GROUP BY spree_products.id具有SUM(count_on_hand)&gt; 0 ORDER BY spree_products_taxons.position ASC LIMIT 12 OFFSET 0

所以必须有一个更好的方法来做到这一点,以便GROUP by子句不会抛弃所有东西。

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

  joins(:master => :prices, variants: :stock_items).where("#{StockItem.quoted_table_name}.count_on_hand > 0 AND #{Product.quoted_table_name}.available_on <= ?", available_on || Time.now).uniq

您可能也希望包含backorderabletrack_inventory - 我不知道您的设置,也许您根本没有这样的变体。

修改:已添加uniq。如果您还想要主变体,那么只需将variants: :stock_items部分更改为variants_including_master: :stock_items