Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用ActiveRecord在Rails中按标签查找所有产品_Sql_Ruby On Rails_Spree_Has Many - Fatal编程技术网

Sql 使用ActiveRecord在Rails中按标签查找所有产品

Sql 使用ActiveRecord在Rails中按标签查找所有产品,sql,ruby-on-rails,spree,has-many,Sql,Ruby On Rails,Spree,Has Many,这可能很简单,但可以说,我正在寻找通过标签检索所有产品的最佳方法。这就是Spree,所以我应该坚持他们建模数据的方式。它实际上是产品和分类,如类别、品牌等 那么,如果产品有且属于多个分类单元,且分类单元有且属于多个产品,那么按分类单元查找所有产品的最佳方法是什么 比如: 。。。但我不确定最后一种方法是什么构成了这个名称。不会分类。通过“分类”查找。产品就足够了吗 编辑:哦,对于多个分类单元,您可以尝试以下方法: Product.find(:all, :include => :product

这可能很简单,但可以说,我正在寻找通过标签检索所有产品的最佳方法。这就是Spree,所以我应该坚持他们建模数据的方式。它实际上是产品和分类,如类别、品牌等

那么,如果产品有且属于多个分类单元,且分类单元有且属于多个产品,那么按分类单元查找所有产品的最佳方法是什么

比如:

。。。但我不确定最后一种方法是什么构成了这个名称。

不会分类。通过“分类”查找。产品就足够了吗

编辑:哦,对于多个分类单元,您可以尝试以下方法:

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
不会分类的。按分类查找。产品就足够了吗

编辑:哦,对于多个分类单元,您可以尝试以下方法:

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

也许你会简单地说如果只有一个分类单元

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

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

也许你会简单地说如果只有一个分类单元

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

@products = @taxons.inject([]) {|taxon| taxon.products}
这将加载产品,以便您可以通过

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

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

@taxon.products

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

通过以下定制,我能够在Spree 2.1.0.beta中实现这一点:

根据这里的答案:

我在/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
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
然后通过将新范围添加到/app/models/spree/base_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
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

这对我来说很有效,而且感觉很轻松。但是,我愿意接受更好的选择。

通过以下定制,我能够在Spree 2.1.0.beta中实现这一点:

根据这里的答案:

我在/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
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
然后通过将新范围添加到/app/models/spree/base_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
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

这对我来说很有效,而且感觉很轻松。但是,我愿意接受更好的选择。

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

范例

Spree::Product.tagged_with("example")
将返回带有标签示例的产品


来源:

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

范例

Spree::Product.tagged_with("example")
将返回带有标签示例的产品


资料来源:

好吧,这将找到一个分类单元,但由于他想按分类单元查找所有产品,我们需要多一点。显然,你需要检查分类单元。按permalink查找,或者使用Taxon。按permalink'categories/'查找。尝试:productsHmm,我不明白为什么不能检索具有给定分类单元的所有产品。嗯,这将找到一个分类单元,但由于他想按分类单元查找所有产品,我们需要更多。显然,您需要检查分类单元。按permalink查找返回了一些东西,或者使用Taxon。按permalink'categories/'查找。尝试:productsHmm,我不明白为什么这不能检索具有给定分类单元的所有产品。超基本:p,现在就沉浸在其中。谢谢你的帮助!这将导致一个N+1查询问题super-basic:p,现在就消失在其中。谢谢你的帮助!这将导致N+1查询问题