狂欢分类关系

时间:2013-03-31 17:45:07

标签: ruby-on-rails activerecord rails-activerecord spree

目标:在分类单元中显示来自另一个taxanomy的分类单元

我拥有的: Taxanomy1'类别'分类[吉他,小提琴,钢琴] Taxanomy2' Brands'分类[Yamaha,Samick,PROEL]

产品有:category&品牌

任务1: 当我在分类单元显示页面时,我想展示@products有

的品牌

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

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

1 个答案:

答案 0 :(得分: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