我有以下模型(我已经剥离了不必要的部分):
class Product < ActiveRecord::Base
has_many :categories, :dependent => :destroy
end
class Category < ActiveRecord::Base
belongs_to :product
has_many :attributes, :dependent => :destroy
end
class Attribute < ActiveRecord::Base
belongs_to :category
attr_accessible :name, :value, :is_key
end
所以基本上, a Product
has manyCategory
和 a Category
has many Attributes
。
我想要的是Product
模型内部的一个方法,它将返回:is_key
设置为 true 的属性。
我尝试了一些变化
def key_attributes
Attribute.joins(:category).where(:attributes => {:is_key => true}, :category => {:product_id => self.id}).all
end
但没有成功。
里面应该key_attributes
有什么?