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
我需要具有某個父類別的所有產品。這是'茶'。例如:'茶/紅茶','茶/水果茶'... –