Rails 3.1 プロジェクトに、次のようなアクティブ レコード モデルがいくつかあるとします。
class Component < ActiveRecord::Base
has_many :bugs
end
class Bug < ActiveRecord::Base
belongs_to :component
belongs_to :project
scope :open, where(:open => true)
scope :closed, where(:open => false)
end
class Project < ActiveRecord::Base
has_many :bugs
has_many :components_with_bugs, :through => :bugs, :conditions => ["bugs.open = ?", true]
end
components_with_bugs
要するに、「スルー」モデルのスコープを設定したい has_many スルー アソシエーション ( ) があります。現在、スコープのコードを複製することでこれを行っています。
単一のデータベース クエリでコンポーネントをロードしながら、スルー モデルでスコープをcomponents_with_bugs
再利用できるように、これを多数のスルー アソシエーション () で定義する方法はありますか? Bug.open
(私はのようなものを想像しています:conditions => Bug.open
)