【问题标题】:Spree Taxons relations狂欢分类群关系
【发布时间】:2013-03-31 17:45:06
【问题描述】:

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

我有什么: Taxanomy1 'Categories' 分类单元 [吉他、小提琴、钢琴] Taxanomy2“品牌”分类群 [Yamaha、Samick、PROEL]

产品兼具:类别和品牌

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

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

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多