0
我想添加一些過濾,以產品,但我的疑問是相當醜陋,重..PostgreSQL的查詢 - 施普雷產品的過濾方法和協會
如何改善這個查詢? (想回到那個沒有翻譯或有,但字段爲空產品)
Spree::Product.all.map { |p| p if !p.translations.find_by(locale: 'en') || (p.translations.find_by(locale: 'en') && p.translations.find_by(locale: 'en').name.blank?) }.reject(&:blank?)
# takes about 1 second
另一個查詢是與產品相關的分類羣:
Spree::Product.all.map { |p| p unless p.taxons.any? }.reject(&:blank?)
我知道有一種方法返回沒有任何關聯記錄的記錄,如:Spree::Product.all.includes(:taggings).where(taggings: { taggable_id: nil })
。但由於某種原因,它不想與Taxons合作。
我還想知道是否有一種方法來改進對模型方法的查詢。例如,我有一個查詢返回所有沒有圖像的產品:
Spree::Product.all.map { |p| p unless p.all_images.any? }.reject(&:blank?)
# takes over 2 seconds
# product_decorator.rb
def all_images
(images + variant_images).uniq
end
謝謝!