私は次のモデルを持っています(不要な部分を取り除いています):
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
つまり、基本的に、 aProductは多数Categoryあり、 aCategoryは多数ありAttributesます。
私が欲しいのは、trueProductに設定された属性を返すモデル内のメソッドです。:is_key
私はいくつかのバリエーションを試しました
def key_attributes
Attribute.joins(:category).where(:attributes => {:is_key => true}, :category => {:product_id => self.id}).all
end
しかし、成功しませんでした。
その中に何を入れるべきkey_attributesですか?