Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a solution that doesn't use Single Table Inheritance for the publications. That means that there are articles, books and chapters tables, instead of one publications table. Here are the commands to run to create the app:</p> <pre><code>$ rails myproject $ cd myproject $ script/generate model book name:string $ script/generate model chapter name:string $ script/generate model article name:string $ script/generate model citation publication_type:string publication_id:integer reference_type:string reference_id:integer </code></pre> <p>Create this file in <code>lib/acts_as_publication.rb</code>:</p> <pre><code>module ActsAsPublication def self.included(base) base.extend(ClassMethods) end module ClassMethods def acts_as_publication has_many :citations, :as =&gt; :publication has_many :references, :as =&gt; :reference, :class_name =&gt; "Citation" end end end </code></pre> <p>Create this file in <code>config/initializers/acts_as_publication.rb</code>:</p> <pre><code>ActiveRecord::Base.send(:include, ActsAsPublication) </code></pre> <p>Then call that in each model, Article, Book and Chapter, like this:</p> <pre><code>class Article &lt; ActiveRecord::Base acts_as_publication end </code></pre> <p>Then add these relationships in <code>app/models/citation.rb</code>:</p> <pre><code>class Citation &lt; ActiveRecord::Base belongs_to :publication, :polymorphic =&gt; true belongs_to :reference, :polymorphic =&gt; true end </code></pre> <p>Now we can create the DB and try it out from the console:</p> <pre><code>$ rake db:migrate $ script/console Loading development environment (Rails 2.2.2) &gt;&gt; a = Article.create!(:name =&gt; "a") =&gt; #&lt;Article id: 1, ...&gt; &gt;&gt; b = Article.create!(:name =&gt; "b") =&gt; #&lt;Article id: 2, ...&gt; &gt;&gt; Citation.create!(:publication =&gt; a, :reference =&gt; b) =&gt; #&lt;Citation id: 1, publication_type: "Article", publication_id: 1, reference_type: "Article", reference_id: 2, created_at: "2009-02-15 13:14:27", updated_at: "2009-02-15 13:14:27"&gt; &gt;&gt; a.citations =&gt; [#&lt;Citation id: 1, ...&gt;] &gt;&gt; a.references =&gt; [] &gt;&gt; b.citations =&gt; [] &gt;&gt; b.references =&gt; [#&lt;Citation id: 1, ...&gt;] &gt;&gt; Book.create!(:name =&gt; "foo") =&gt; #&lt;Book id: 1, name: "foo", created_at: "2009-02-15 13:18:23", updated_at: "2009-02-15 13:18:23"&gt; &gt;&gt; a.citations.create(:reference =&gt; Book.first) =&gt; #&lt;Citation id: 2, publication_type: "Article", publication_id: 1, reference_type: "Book", reference_id: 1, created_at: "2009-02-15 13:18:52", updated_at: "2009-02-15 13:18:52"&gt; &gt;&gt; Book.first.references =&gt; [#&lt;Citation id: 2, ...&gt;] &gt;&gt; a.citations =&gt; [#&lt;Citation id: 1, publication_type: "Article", publication_id: 1, reference_type: "Article", reference_id: 2, created_at: "2009-02-15 13:14:27", updated_at: "2009-02-15 13:14:27"&gt;, #&lt;Citation id: 2, publication_type: "Article", publication_id: 1, reference_type: "Book", reference_id: 1, created_at: "2009-02-15 13:18:52", updated_at: "2009-02-15 13:18:52"&gt;] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload