私は、サイト間の相互公開の概念を持つマルチサイト CMS に取り組んでいます。いくつかの種類のコンテンツ (記事、イベント、略歴など) を多くのサイトに関連付けることができ、サイトには多くのコンテンツを含めることができます。コンテンツとサイトの間の多対多の関連付けは、関連付けられた各コンテンツ アイテムのいくつかの共通属性もサポートする必要があります。特定の関連付けられたサイトの特定のコンテンツの「プライマリ」および「セカンダリ」コンテンツ ステータス。
私の考えは、ContentAssociation と呼ばれるポリモーフィック結合モデルを作成することでしたが、ポリモーフィック アソシエーションを期待どおりに動作させるのに問題があり、おそらくこれがすべて間違っているのではないかと考えています。
結合テーブルとモデルのセットアップは次のとおりです。
create_table "content_associations", :force => true do |t|
t.string "associable_type"
t.integer "associable_id"
t.integer "site_id"
t.boolean "primary_eligible"
t.boolean "secondary_eligible"
t.boolean "originating_site"
t.datetime "created_at"
t.datetime "updated_at"
end
class ContentAssociation < ActiveRecord::Base
belongs_to :site
belongs_to :associable, :polymorphic => true
belongs_to :primary_site, :class_name => "Site", :foreign_key => "site_id"
belongs_to :secondary_site, :class_name => "Site", :foreign_key => "site_id"
belongs_to :originating_site, :class_name => "Site", :foreign_key => "site_id"
end
class Site < ActiveRecord::Base
has_many :content_associations, :dependent => :destroy
has_many :articles, :through => :content_associations, :source => :associable, :source_type => "Article"
has_many :events, :through => :content_associations, :source => :associable, :source_type => "Event"
has_many :primary_articles, :through => :content_associations,
:source => :associable,
:source_type => "Article",
:conditions => ["content_associations.primary_eligible = ?" true]
has_many :originating_articles, :through => :content_associations,
:source => :associable,
:source_type => "Article",
:conditions => ["content_associations.originating_site = ?" true]
has_many :secondary_articles, :through => :content_associations,
:source => :associable,
:source_type => "Article",
:conditions => ["content_associations.secondary_eligible = ?" true]
end
class Article < ActiveRecord::Base
has_many :content_associations, :as => :associable, :dependent => :destroy
has_one :originating_site, :through => :content_associations,
:source => :associable,
:conditions => ["content_associations.originating_site = ?" true]
has_many :primary_sites, :through => :content_associations,
:source => :associable
:conditions => ["content_associations.primary_eligible = ?" true]
has_many :secondary_sites, :through => :content_associations,
:source => :associable
:conditions => ["content_associations.secondary_eligible = ?" true]
end
上記の関連付け宣言の多くのバリエーションを試しましたが、何をしても、私が望む動作を得ることができないようです
@site = Site.find(2)
@article = Article.find(23)
@article.originating_site = @site
@site.originating_articles #=>[@article]
またはこれ
@site.primary_articles << @article
@article.primary_sites #=> [@site]
Rails のビルトイン ポリモーフィズムは、サイトとコンテンツのさまざまな部分との間のこれらの接続に影響を与えるために使用するメカニズムが間違っているのでしょうか? 複数の異なるモデルを 1 つの共通モデルに多対多で接続する必要があるため、便利なようですが、この方法で使用する例を見つけるのに苦労しました。
おそらく、複雑さの一部は、双方向の関連付けが必要なことです。つまり、特定の記事が関連付けられているすべてのサイトを表示し、特定のサイトに関連付けられているすべての記事を表示します。プラグインhas_many_polymorphsについて聞いたことがありますが、問題が解決するようです。しかし、ここで Rails 3 を使用しようとしていますが、まだサポートされているかどうかはわかりません。
このコンテキストでのポリモーフィズムの使用に関する私の不完全な理解に光を当てるだけであっても、どんな助けも大歓迎です。
前もって感謝します!