1
기사에는이 기사가 언급 한 많은 기사가 있으며 다른 많은 기사에서이 기사를 참조 할 수 있습니다. 때로는 기사가 해당 기사를 참조 할 수도 있습니다.레일 및 ActiveRecord에서 참조 및 인용이있는 기사를 어떻게 모델링 하시겠습니까?
기사에는이 기사가 언급 한 많은 기사가 있으며 다른 많은 기사에서이 기사를 참조 할 수 있습니다. 때로는 기사가 해당 기사를 참조 할 수도 있습니다.레일 및 ActiveRecord에서 참조 및 인용이있는 기사를 어떻게 모델링 하시겠습니까?
나는 이런 식으로 할 거라고 :
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