編集された回答
Rails は、ポリモーフィック タイプ列でフィルター処理を行うと、ポリモーフィック アソシエーションのイーガー ロードをサポートすることが最近わかりました。したがって、偽の関連付けを宣言する必要はありません。
class Container
belongs_to :content, :polymorphic => true
end
Container
次に、 by をクエリしますcontainer_type
。
containers_with_food = Container.find_all_by_content_type("Food",
:include => :content)
containers_with_thing = Container.find_all_by_content_type("Thing",
:include => :content)
古い回答
1 つのクエリにポリモーフィック オブジェクトを直接含める方法がないため、これはハックです。
class Container
belongs_to :contents, :polymorphic => true
# add dummy associations for all the contents.
# this association should not be used directly
belongs_to :food
belongs_to :thing
end
Container
次に、 by をクエリしますcontainer_type
。
containers_with_food = Container.find_all_by_content_type("Food",
:include => :food)
containers_with_thing = Container.find_all_by_content_type("Thing",
:include => :thing)
その結果、データベースへの 2 つの SQL 呼び出しが発生します (レールはすべてに対して 1 つの SQL を実行するため、実際には 4 つの呼び出しです:include
) 。
コンテンツ タイプごとに異なる列セットが必要なため、1 つの SQL でこれを行う方法はありません。
警告:クラスのダミーの関連付けContent
は、予期しない結果になるため、直接使用しないでください。
contents
例:テーブルの最初のオブジェクトに食べ物が含まれているとします。
Content.first.food # will work
Content.first.thing
2 番目の呼び出しは機能しません。が指すオブジェクトThing
と同じ ID を持つオブジェクトが得られる場合があります。Food
Content