Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The migration has no knowledge of how you intend to use the tables defined within it. All that information goes in the models.</p> <p>In short your migration is fine for what you've described will be done. But you need to flesh out the models to define the associations you're thinking of.</p> <pre><code>class Comic &lt; ActiveRecord::Base belongs_to :writer, :class_name =&gt; "Person" belongs_to :artist, :class_name =&gt; "Person" end </code></pre> <p>This allows you to reference the Writer and Artist from a Comic. However, you will probably want to reciprocate the association so that you can easily fetch a comic from a person based on their role in it's production.</p> <pre><code>class Person &lt; ActiveRecord::Base has_many :comics_as_writer, :class_name =&gt; "Comic", :foreign_key =&gt; :writer_id has_many :comics_as_artist, :class_name =&gt; "Comic", :foreign_key =&gt; :artist_id # some times you don't care what a person did for a comic, # you just want to know what they worked on. has_many :comics, :finder_sql =&gt; "SELECT comics.* FROM comics, people WHERE " + "`comics`.`writer_id` = `people`.`id` OR " + " `comics`.`artist_id` = `people`.`id`" end </code></pre> <p>With these relationships defined the following is possible:</p> <pre><code>@comic.artist # =&gt; Person with id matching comics.artist_id @comic.writer # =&gt; Person with id matching comics.writer_id @person.comics_as_writer # =&gt; Array of comics where @person.id matches comics.writer_id @person.comics_as_artist # =&gt; Array of comics where @person.id matches comics.artist_id @person.comics # =&gt; Array of comics where @person.id matches comics.writer_id or comics.artist_id </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.
 

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