Possible Duplicate:
Unable to join self-joins tables in Rails范围在Rails和嵌套查询
我多类别
class Category < ActiveRecord::Base
belongs_to :parent, :class_name => "Category", :foreign_key => "parent_id"
has_many :children, :class_name => "Category"
has_many :products
attr_accessible :description, :title, :parent
end
这里是Product
class Product < ActiveRecord::Base
belongs_to :category
end
一个模式,我需要在Product
定义范围,能够找到所有产品按父类别名称
class Product < ActiveRecord::Base
#.....
#scope :of_tea, lambda{ where(:category.parent.name => "tea") } # not working
end
我想你的意思是在指定的外键:孩子的关系,而不是:父。 – cdesrosiers
我需要具有某个父类别的所有产品。这是'茶'。例如:'茶/红茶','茶/水果茶'... –