Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To access the images from an article you need to add an association in the Article class:</p> <pre><code>class Article has_many :images, :foreign_key =&gt; :association_id end </code></pre> <p>You mentioned that sometimes images are mapped to travel_guides and slideshows (2 additional tables). You have to add an association for each table. So you might do something like:</p> <pre><code>class Article &lt; ActiveRecord::Base has_many :images, :foreign_key =&gt; :association_id has_many :travel_guides, :foreign_key =&gt; :association_id has_many :slideshow_images, :foreign_key =&gt; :association_id end </code></pre> <p>It is generally also a good idea to set up the reverse associations:</p> <pre><code>class Image &lt; ActiveRecord::Base belongs_to :article, :foreign_key =&gt; :association_id end class TravelGuide &lt; ActiveRecord::Base belongs_to :article, :foreign_key =&gt; :association_id end class SlideshowImage &lt; ActiveRecord::Base set_table_name :slideshows belongs_to :article, :foreign_key =&gt; :association_id end </code></pre> <p>Some things to consider:</p> <ul> <li><p>I left off the :dependent option in the *has_many* associations. This is generally not a good idea. You can set <em>:depedent => :destroy</em> so that you can call some cleanup logic in your image classes (to delete the image files).</p></li> <li><p>If you do not have simple joins, you should consider adding the <em>:conditions</em> option to the has_many (or the belongs to). </p></li> <li><p>Single table inheritance and or polymorhpic associations are both other tools you can use if the above suggestions don't solve all of your problems.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    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