ruby-on-rails – 获取所有类别和子类别的产品(rails,awesome_nested_set)

有一个正在开发的电子商务应用程序我试图解决以下问题:
我通过awesome_nested_set插件实现了我的类别.
如果我通过选择一个类别列出我的文章一切正常,但对于某些链接,我想显示一个类别的所有产品和其子类别的产品.

这里只有一个类别的控制器代码可以正常工作:

# 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

我注释掉的一行是我在类别模型中自己编写的辅助方法,它返回类别中所有产品及其子类别的数组.

它的定义如下:

# app/models/category.rb
def product_list
  self_and_ancestors.to_a.collect! { |x| x.products }
end

现在,当我取消注释这一行并尝试选择一个类别时,我的产品控制器代码会出现错误,例如

undefined method `order' for #<Array:0x1887c2c>

要么

undefined method `page' for #<Array:0x1887c2c>

因为我正在使用订购和分页,它不能再订购.

有什么想法如何在我的控制器中的ActiveRecord Relation元素中获取所有产品?
谢谢

UPDATE

所以,当我使用以下内容时:

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我得到以下错误:

Mysql::Error: Unknown column 'products.category_id' in 'where clause': SELECT `products`.* FROM `products` WHERE `products`.`category_id` IN (6,8,9)

我如何获得这个星座的所有产品?

更新2

好的,所以我要开始赏金.

如果我尝试:

def all_products
Categorization.find(:all,:conditions => {:category_id => branch_ids})
结束

我再次为#`获取undefined methodorder’
我需要知道如何将many_to_many关系的所有产品作为ActiveRecord关系.

更新3

我把相关的代码放在一个要点
https://gist.github.com/1211231

解决方法

awesome_nested_set的关键是使用lft列中的范围.
这是我如何使用直接关联(类别has_many文章)的代码示例
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

然后在控制器的某个地方

@category = Category.find_by_name("fruits")
@articles = Article.tree(@category)

将找到苹果,橘子,香蕉等类别下的所有文章.
你应该通过加入分类来调整这个想法(但你确定你需要多对多的关系吗?)

无论如何我会尝试这个:

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.rgt
        ])
      end
end

如果有任何问题,请告诉我

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...