【问题标题】:Find all Products by Tag in Rails with ActiveRecord使用 ActiveRecord 在 Rails 中按标签查找所有产品
【发布时间】:2010-01-14 00:04:25
【问题描述】:

这可能是非常简单的事情,但我正在寻找按标签检索所有产品的最佳方式,可以这么说。这是 Spree,所以我应该坚持他们对数据建模的方式。其实就是ProductTaxon(比如品类、品牌等)

那么,如果 Product has_and_belongs_to_many :taxons 和 Taxon has_and_belongs_to_many :products,按分类查找所有产品的最佳方法是什么?

类似:

@taxon = Taxon.find_by_permalink('categories/')
@products = Product.find_by_taxon(@taxon)

...但我不确定最后一种方法是什么(只是编了个名字)。

【问题讨论】:

    标签: sql ruby-on-rails spree has-many


    【解决方案1】:

    您可能只是简单地说是否只有一个分类单元

    @products = @taxon.products
    

    如果有多个,我们需要稍微不同的方法。但即使那样你也可以

    @products = @taxons.inject([]) {|taxon| taxon.products}
    

    【讨论】:

    • 超级基本的:p,现在完全迷失了。感谢您的帮助!
    • 这会导致N+1查询问题
    【解决方案2】:
    @taxon = Taxon.find_by_permalink('categories', :include => :products) 
    

    这将预先加载产品,以便您可以通过

    @taxon.products
    

    不会再次访问数据库。这是仅使用 .products 的更有效形式,可避免 N+1 查询问题。

    【讨论】:

      【解决方案3】:

      Taxon.find_by_permalink('categories/').products 不够吗?

      编辑:哦,对于多个分类单元,您可以尝试这样的事情:

      Product.find(:all, :include => :products_taxons, :conditions => { :products_taxons => {:taxon_id => [1,2,3]} }) # will find products with taxons with id 1, 2 or 3
      

      【讨论】:

      • 嗯,这会找到一个分类单元,但由于他想找到分类单元的所有产品,我们需要更多。
      • 显然您需要检查 Taxon.find_by_permalink 是否返回了某些内容,或者使用 Taxon.find_by_permalink('categories/').try(:products)
      • 嗯,我不明白为什么它不能检索具有给定分类单元的所有产品。
      【解决方案4】:

      通过以下自定义,我能够在 Spree 2.1.0.beta 中使用它:

      根据这里的答案:Finding records with two specific records in another table

      我在 /app/models/spree/product_decorator.rb 中添加了一个新的产品范围

      Spree::Product.class_eval do
        add_search_scope :in_all_taxons do |*taxons|
          taxons = get_taxons(taxons)
          id = arel_table[:id]
          joins(:taxons).where(spree_taxons: { id: taxons }).group(id).having(id.count.eq(taxons.size))
        end
      end
      

      然后通过将其添加到 /app/models/spree/base_decorator.rb 来使用新范围

      Spree::Core::Search::Base.class_eval do
          def get_base_scope
            base_scope = Spree::Product.active
            base_scope = base_scope.in_all_taxons(taxon) unless taxon.blank?
            base_scope = get_products_conditions_for(base_scope, keywords)
            base_scope = add_search_scopes(base_scope)
            base_scope
          end
      end
      

      现在我可以使用标准搜索助手来检索产品(这意味着我仍然可以提供关键字等以及多个分类单元):

      # taxon_ids is an array of taxon ids
      @searcher = build_searcher(params.merge(:taxon => taxon_ids))
      @products = @searcher.retrieve_products
      

      这对我有用,感觉很轻松。不过,我愿意接受更好的选择。

      【讨论】:

        【解决方案5】:

        如果您想通过标签查找产品,可以使用tagged_with

        例子

        Spree::Product.tagged_with("example")
        

        将返回带有“example”标签的产品

        来源:https://github.com/mbleigh/acts-as-taggable-on

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-09
          • 1970-01-01
          • 1970-01-01
          • 2020-10-24
          相关资源
          最近更新 更多