您如何使用rails& amp;中的引用和引用对文章进行建模? ActiveRecord的?

时间:2009-02-01 00:49:18

标签: ruby-on-rails database-design activerecord

一篇文章引用了许多文章,许多其他文章都可以引用它。有时文章可以引用也引用它的文章。

1 个答案:

答案 0 :(得分:3)

我会这样做:

class Article < ActiveRecord::Base
  # mentions in other articles
  has_many :references, :foreign_key => 'referred_article_id'
  # articles that refer to it
  has_many :referrers, :through => :references, :foreign_key => 'referred_article_id'
  # articles it refers to
  has_many :referred_articles, :through => :references, :foreign_key => 'referrer_id'
end

class Reference < ActiveRecord::Base
  belongs_to :referrer, :class => Article
  belongs_to :referred_article, :class => Article
end