你将如何模型中轨与放大器的参考和引用的文章; ActiveRecord的? [英] How would you model articles with references and citations in rails & ActiveRecord?

查看:136
本文介绍了你将如何模型中轨与放大器的参考和引用的文章; ActiveRecord的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一篇文章都有它指的是和许多其他文章可以参考了很多文章。有时候一篇文章可以参考的一篇文章还提到了它。

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.

推荐答案

我会做这样的:

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

这篇关于你将如何模型中轨与放大器的参考和引用的文章; ActiveRecord的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆