整蛊活动记录的关系 - 多态的双向自我指涉 [英] Tricky active record relationships - polymorphic bi-directional self-referential
问题描述
你会如何模拟引用和引文的出版物(文章,书籍,章节,等...)?
How would you model the references and citations to publications (articles, books, chapters, etc...)?
一个出版物可以是一篇文章,书籍或一章,它有许多参考其他出版物和其他出版物引用它(调用这些引文)
A publication can be an article, book or a chapter and it has many references to other publications and other publications refer to it (call these citations)
我需要能够列出出版物之间的关系:从其他出版物在出版物引用和引文本刊物
I need to be able to list the relationships among the publications: The references in a publication and the citations from other publications to this publication
我最初的理解是,这将是一个多态的关系,处理不同类型的出版物,它需要一个bidirectionalself加入。
My initial understanding is that this would be a polymorphic relationship to handle the different types of publications and that it would require a bidirectionalself join.
我吧刺
Publication
belongs_to :writing, :polymorphic =>true
has_and_belongs_to_many :references
:class_name => "Publication"
:join_table => 'reference_citation'
:foreign_key => 'reference_id'
:foreign_key => 'citation_id'
Book, Chapter, Article all have:
has_many :publications :as =>writing
我觉得这有点混乱,所以任何建议,这将有助于澄清这将是巨大的。即使对象的字段命名建议。
I find this a bit confusing so any suggestions that would help clarify it would be great. Even object and field naming suggestions.
[我问了一个不太清楚这个版本的问题<一href="http://stackoverflow.com/questions/499934/how-would-you-model-articles-with-references-and-citations-in-rails-activerecord">here.]
[I asked a less clear version of this question here.]
我还可能需要使用有许多通过,因为我需要破坏关系的能力
I also probably need to use has many through because I will need the ability to destroy the relationship
推荐答案
下面是一个使用单表继承使用自引用关系的解决方案。使用这些命令来创建应用程序:
Here's a solution using a self-referential relationship using single table inheritance. Use these commands to create the app:
$ rails myproject
$ cd myproject
$ script/generate model publication type:string name:string
$ script/generate model citation publication_id:integer reference_id:integer
该设置的关系是这样的:
The setup the relationships this way:
class Publication < ActiveRecord::Base
has_many :citations
has_many :cited_publications, :through => :citations, :source => :reference
has_many :references, :foreign_key => "reference_id", :class_name => "Citation"
has_many :refered_publications, :through => :references, :source => :publication
end
class Citation < ActiveRecord::Base
belongs_to :publication
belongs_to :reference, :class_name => "Publication"
end
class Article < Publication
end
class Book < Publication
end
class Chapter < Publication
end
现在,我们可以创建数据库,并尝试从控制台:
Now we can create the DB and try it out from the console:
$ rake db:migrate
$ script/console
Loading development environment (Rails 2.2.2)
>> a = Article.create!(:name => "Article")
=> #<Article id: 1, ...>
>> b = Book.create!(:name => "Book")
=> #<Book id: 2, ...>
>> a.citations.create(:reference => b)
=> #<Citation id: 1, publication_id: 1, reference_id: 2, created_at: "2009-02-15 14:13:15", updated_at: "2009-02-15 14:13:15">
>> a.citations
=> [#<Citation id: 1, ...>]
>> a.references
=> []
>> b.citations
=> []
>> b.references
=> [#<Citation id: 1, publication_id: 1, reference_id: 2, created_at: "2009-02-15 14:13:15", updated_at: "2009-02-15 14:13:15">]
>> a.cited_publications
=> [#<Book id: 2, type: "Book", name: "Book", created_at: "2009-02-15 14:11:00", updated_at: "2009-02-15 14:11:00">]
>> a.refered_publications
=> []
>> b.cited_publications
=> []
>> b.refered_publications
=> [#<Article id: 1, type: "Article", name: "Article", created_at: "2009-02-15 14:10:51", updated_at: "2009-02-15 14:10:51">]
这篇关于整蛊活动记录的关系 - 多态的双向自我指涉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!