私のProduct
モデルには次の関係があります。
has_many :features, :class_name => 'ProductFeature', :source => :product_feature, :include => :feature
だから私はできるProduct.features
これは正常に動作します。feature
しかし、必要に応じて、テーブル内のフィールドでそれをフィルタリングできるようにしたいと考えています。たとえば、擬似コードでは次のようになります。
find all product features where feature is comparable
compare
の bool フィールドfeature
です。
私は2時間しっかりと試してきましたが、それを理解できません(新しいクエリを完全に作成しないと)。フィールドでのみフィルタリングできるように見えるためfeature
、リレーションからテーブルのフィールドにアクセスする方法がわかりません。Product.features
product_features
これは私がこれまでに思いついたものです:
def features_compare
features.feature.where(:compare => true)
end
feature
しかし、私が理解している有効な方法ではないと言っているだけです。
編集
関係がより明確になるように、モデルを更新しました。
product.rb:
class Product < ActiveRecord::Base
belongs_to :company
belongs_to :insurance_type
has_many :product_features
has_many :reviews
attr_accessible :description, :name, :company
end
product_feature.rb:
class ProductFeature < ActiveRecord::Base
belongs_to :product
belongs_to :feature
delegate :name, :to => :feature
attr_accessible :value
end
機能.rb
class Feature < ActiveRecord::Base
attr_accessible :name, :compare
end
product_features
に属し、product
がfeature
どこにFeature.compare
あるかを照会できるようにしたいと考えていますtrue
。このようなもの:
product.rb
def features_compare
product_features.where(:compare => true)
end
ではなくモデルcompare
内にあるため、これはエラーをスローします。product_feature.rb で次のことを試しました。Feature
ProductFeature
delegate :compare, :to => :feature
しかし、私は助けませんでした。
数時間以内にこれに賞金を追加しますので、助けてください!