1

目标:显示分类单元内另一个分类的分类单元

我所拥有的:Taxanomy1 'Categories' 分类单元 [吉他、小提琴、钢琴] Taxanomy2 '品牌' 分类单元 [Yamaha、Samick、PROEL]

产品兼具:品类和品牌

任务1:当我在分类显示页面上时,我想展示@products 拥有的品牌

任务2:当我在品牌页面时,我想显示当前品牌产品的类别

对不起,如果我的解释不够清楚

4

1 回答 1

1
taxon_id = 558398765 # "Current" Taxon
brand_taxonomy_id = 854451436 # Your "Brand" Taxonomy
sql_query  = <<-SQL
SELECT DISTINCT "spree_products_taxons"."taxon_id" FROM "spree_products_taxons" WHERE "spree_products_taxons"."product_id" IN (
  SELECT "spree_products"."id" FROM "spree_products"
  INNER JOIN "spree_products_taxons" ON "spree_products"."id" = "spree_products_taxons"."product_id"
  WHERE "spree_products_taxons"."taxon_id" = #{taxon_id}
)
SQL
taxon_ids = ActiveRecord::Base.connection.execute(sql_query).to_a.map {|tp| tp['taxon_id']}
Taxon.where(id: taxon_ids, taxonomy_id: brand_taxonomy_id).all # All "Brand" taxons for all products in your "Current" Taxon

Task2 相同,将最后一行更改为:

Taxon.where(id: taxon_ids, taxonomy_id: category_taxonomy_id).all # All "Category" taxons for all products in your "Brand" Taxon
于 2013-04-04T08:02:13.787 回答