How would you model articles with references and citations in rails & ActiveRecord?

岁酱吖の 提交于 2019-12-11 07:46:12

问题


An article has many articles that it refers to and many other articles can refer to it. Sometimes an article can refer to an article that also refers to it.


回答1:


I'd do it like this:

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


来源:https://stackoverflow.com/questions/499934/how-would-you-model-articles-with-references-and-citations-in-rails-activereco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!