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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 获取所有类别和子类别的产品(rails,awesome_nested_set)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有一个正在开发的电子商务应用程序我试图解决以下问题:
我通过awesome_nested_set插件实现了我的类别.
如果我通过选择一个类别列出我的文章一切正常,但对于某些链接,我想显示一个类别的所有产品和其子类别的产品.

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

  1. # products_controller.rb
  2. def index
  3. if params[:category]
  4. @category = Category.find(params[:category])
  5. #@products = @category.product_list
  6. @products = @category.products
  7. else
  8. @category = false
  9. @products = Product.scoped
  10. end
  11. @products = @products.where("title like ?","%" + params[:title] + "%") if params[:title]
  12. @products = @products.order("created_at).page(params[:page]).per( params[:per_page] ? params[:per_page] : 25)
  13. @categories = Category.all
  14. end

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

它的定义如下:

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

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

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

要么

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

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

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

UPDATE

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

  1. class Category < ActiveRecord::Base
  2. acts_as_nested_set
  3.  
  4. attr_accessible :name,:description,:lft,:rgt,:parent_id
  5.  
  6. has_many :categorizations
  7. has_many :products,:through => :categorizations
  8.  
  9. attr_accessor :product_list
  10.  
  11. def branch_ids
  12. self_and_descendants.map(&:id).uniq
  13. end
  14.  
  15. def all_products
  16. Product.find(:all,:conditions => { :category_id => branch_ids } )
  17. end
  18.  
  19.  
  20. end

并询问控制器@ category.all_products我得到以下错误

  1. 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文章)的代码示例
  1. module Category
  2. extend ActiveSupport::Concern
  3. included do
  4. belongs_to :category
  5. scope :sorted,includes(:category).order("categories.lft,#{table_name}.position")
  6. end
  7.  
  8. module ClassMethods
  9. def tree(category=nil)
  10. return scoped unless category
  11. scoped.includes(:category).where([
  12. "categories.tree_id=1 AND categories.lft BETWEEN ? AND ?",category.lft,category.rgt
  13. ])
  14. end
  15. end # ClassMethods
  16. end

然后在控制器的某个地方

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

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

无论如何我会尝试这个:

  1. class Product < AR
  2. has_many :categorizations
  3.  
  4. def self.tree(category=nil)
  5. return scoped unless category
  6. select("distinct(products.id),products.*").
  7. joins(:categorizations => :category).where([
  8. "categories.lft BETWEEN ? AND ?",category.rgt
  9. ])
  10. end
  11. end

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

猜你在找的Ruby相关文章