Rails中的双向多态连接模型?

tre*_*sen 3 activerecord ruby-on-rails polymorphic-associations ruby-on-rails-3

我正在开发一个多站点CMS,它具有站点间交叉发布的概念.几种类型的内容(文章,事件,Bios等)可以与许多网站相关联,而网站可以包含许多内容.内容片段和站点之间的多对多关联还必须支持每个相关内容项的一些共同属性 - 站点起源的概念(这是内容出现的原始站点吗?)以及概念给定关联网站上给定内容的"主要"和"次要"内容状态.

我的想法是创建一个名为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
Run Code Online (Sandbox Code Playgroud)

我已经尝试了上述关联声明的很多变化,但不管我做什么,我似乎无法得到我想要的行为

@site = Site.find(2)
@article = Article.find(23)
@article.originating_site = @site
@site.originating_articles #=>[@article]
Run Code Online (Sandbox Code Playgroud)

或这个

@site.primary_articles << @article
@article.primary_sites #=> [@site]
Run Code Online (Sandbox Code Playgroud)

Rails的内置多态性是否用于影响站点及其各种内容之间的这些连接的错误机制?它似乎很有用,因为我需要以多对多的方式将多个不同的模型连接到单个通用模型,但我很难找到以这种方式使用它的任何示例.

也许复杂性的一部分是我需要双向关联 - 即查看给定文章所关联的所有网站,并查看与给定网站关联的所有文章.我听说过插件has_many_polymorphs,看起来它可以解决我的问题.但是我在这里尝试使用Rails 3并且不确定它是否支持.

非常感谢任何帮助 - 即使它只是更多地阐明了我对多态性在这种情况下的使用的不完全理解.

提前致谢!

Ada*_*sek 9

如果您需要关联比STI允许的更具可扩展性,您可以尝试编写自己的集合助手,进行额外的类型自省.

任何时候你定义有关系belongs_to,has_many或者has_one等你也可以定义与该集合辅助功能:

class Article < ActiveRecord::Base
  has_many :associations, :as => :associable, :dependent => :destroy
  has_many :sites, :through => :article_associations

  scope :originating_site, lambda { joins(:article_associations).where('content_associations.originating_site' => true).first }
  scope :primary_sites, lambda { joins(:article_associations).where('content_associations.primary_eligable' => true) }
  scope :secondary_sites, lambda { joins(:article_associations).where('content_associations.secondary_eligable' => true) }
end

class Site < ActiveRecord::Base
  has_many :content_associations, :as => :associable, :dependent => :destroy do
    def articles
      collect(&:associable).collect { |a| a.is_a? Article }
    end
  end
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
Run Code Online (Sandbox Code Playgroud)

如果你需要它们更干,你可以将这些函数defs移动到其他地方:

module Content
  class Procs
    cattr_accessor :associations
    @@associations = lambda do
      def articles
        collect(&:associable).collect { |a| a.is_a? Article }
      end

      def events
        collect(&:associable).collect { |e| e.is_a? Event }
      end

      def bios
        collect(&:associable).collect { |b| b.is_a? Bio }
      end
    end
  end
end


class Site < ActiveRecord::Base
  has_many :content_associations, :as => :associable, :dependent => :destroy, &Content::Procs.associations
end
Run Code Online (Sandbox Code Playgroud)

而且由于这个例子中的文章,事件和bios都在做同样的事情,我们可以更多地干这个:

module Content
  class Procs
    cattr_accessor :associations
    @@associations = lambda do
      %w(articles events bios).each do |type_name|
        type = eval type_name.singularize.classify
        define_method type_name do
          collect(&:associable).collect { |a| a.is_a? type }
        end
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

现在它开始变得更像通用插件,而不是特定于应用程序的代码.这很好,因为您可以轻松地重复使用它.