获得类别和子类别的所有产品(轨,awesome_nested_set) [英] get all products of category and child categories (rails, awesome_nested_set)
问题描述
有正在开发的电子商务应用程序,我试图让我的头围绕以下几个问题: 我有我的类别通过awesome_nested_set插件实现。 如果我通过选择一个类别一切正常列出我的文章,但由于某些环节我想要显示的一个类别中的所有产品和它的子类的产品。
having an e-commerce application under development i am trying to get my head around the following problem: I have my categories realized through the awesome_nested_set plugin. If I list my articles through selecting one category everything works fine, but for some links I want to show all the products of one category and the products of its child categories.
下面是控制器code,与只有一类正常工作:
here is the controller code that works fine with only one category:
# products_controller.rb
def index
if params[:category]
@category = Category.find(params[:category])
#@products = @category.product_list
@products = @category.products
else
@category = false
@products = Product.scoped
end
@products = @products.where("title like ?", "%" + params[:title] + "%") if params[:title]
@products = @products.order("created_at).page(params[:page]).per( params[:per_page] ? params[:per_page] : 25)
@categories = Category.all
end
我注释掉该行是一个辅助方法,我在类别模型返回类别的所有产品和它的子类在数组中写我自己。
The line I commented out is a helper method I wrote myself in the category model which returns all products of the category and its child categories in an array.
有被定义如下:
# app/models/category.rb
def product_list
self_and_ancestors.to_a.collect! { |x| x.products }
end
现在,当我取消这条线,并尝试选择一个类别我的产品控制器code有错误的休息,比如
Now when I uncomment this line and try to select one category my products controller code breaks with errors like
undefined method `order' for #<Array:0x1887c2c>
或
undefined method `page' for #<Array:0x1887c2c>
由于我使用的排序和分页,它不能命令arary了。
because I am using ordering and pagination and it can't order the arary anymore.
任何想法如何让所有的产品在我的控制器一个ActiveRecord关联的元素? 谢谢
Any ideas how to get all the products in an ActiveRecord Relation element in my controller? thanks
更新
所以,当我使用了以下内容:
so, when I use the following:
class Category < ActiveRecord::Base
acts_as_nested_set
attr_accessible :name, :description, :lft, :rgt, :parent_id
has_many :categorizations
has_many :products, :through => :categorizations
attr_accessor :product_list
def branch_ids
self_and_descendants.map(&:id).uniq
end
def all_products
Product.find(:all, :conditions => { :category_id => branch_ids } )
end
end
和要求控制器 @ category.all_products
我收到以下错误:
and ask the controller for @category.all_products
i get the following error:
Mysql::Error: Unknown column 'products.category_id' in 'where clause': SELECT `products`.* FROM `products` WHERE `products`.`category_id` IN (6, 8, 9)
我怎么会得到所有产品,这个星座的?
How would I get all products with this constellation?
更新2
好了,我要开始一个赏金。
Ok, so I am going to start a bounty.
如果我尝试:
高清all_products Categorization.find(:所有,:条件=> {:CATEGORY_ID => branch_ids}) 结束
def all_products Categorization.find(:all, :conditions => { :category_id => branch_ids } ) end
我得到再次未定义的方法
序为#`
我需要知道我怎样才能得到一个MANY_TO_MANY关系作为一个ActiveRecord关系的所有产品。
I get again undefined method
order' for #`
I need to know how I can get all the products of a many_to_many relation as an ActiveRecord relation.
更新3
我把相关的code的要点 https://gist.github.com/1211231
I put the relevant code in a gist https://gist.github.com/1211231
推荐答案
与awesome_nested_set关键是要在 LFT列使用范围。 下面是我如何做到这一点有直接关联一个code样品(类别的has_many篇)
The key with awesome_nested_set is to use a range in the lft column. Here's a code sample of how I do it with a direct association (category has_many articles)
module Category
extend ActiveSupport::Concern
included do
belongs_to :category
scope :sorted, includes(:category).order("categories.lft, #{table_name}.position")
end
module ClassMethods
def tree(category=nil)
return scoped unless category
scoped.includes(:category).where([
"categories.tree_id=1 AND categories.lft BETWEEN ? AND ?",
category.lft, category.rgt
])
end
end # ClassMethods
end
然后在某处控制器
Then somewhere in a controller
@category = Category.find_by_name("fruits")
@articles = Article.tree(@category)
这会发现在类别苹果,桔子,香蕉,等等等等所有文章 你应该适应这一想法与参加关于分类已(但你确定你需要一个很多人在这里多的关系?)
that will find all articles under the categories apples, oranges, bananas, etc etc. You should adapt that idea with a join on categorizations (but are you sure you need a many to many relationship here?)
无论如何,我会尝试这样的:
Anyway I would try this :
class Product < AR
has_many :categorizations
def self.tree(category=nil)
return scoped unless category
select("distinct(products.id), products.*").
joins(:categorizations => :category).where([
"categories.lft BETWEEN ? AND ?", category.lft, category.rgt
])
end
end
让我知道如果有任何疑难杂症
Let me know if there's any gotcha
这篇关于获得类别和子类别的所有产品(轨,awesome_nested_set)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!