Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I didn't use it, but I googled around and looked into Rails sources and I think that what you're looking for is <code>:foreign_type</code>. Try it and tell if it works :)</p> <pre><code>has_one :secondary_photo, :as =&gt; :attachable, :class_name =&gt; "Photo", :dependent =&gt; :destroy, :foreign_type =&gt; 'SecondaryPost' </code></pre> <p>I think that type in your question should be <code>Post</code> instead of <code>Photo</code> and, respectively, it would be better to use <code>SecondaryPost</code> as it assigned to <code>Post</code> model.</p> <p>EDIT:</p> <p>Above answer is completly wrong. <code>:foreign_type</code> is availble in polymorphic model in <code>belongs_to</code> association to specify name of the column that contains type of associated model.</p> <p>As I look in Rails sources, this line sets this type for association:</p> <pre><code>dependent_conditions &lt;&lt; "#{reflection.options[:as]}_type = '#{base_class.name}'" if reflection.options[:as] </code></pre> <p>As you can see it uses <code>base_class.name</code> to get type name. As far as I know you can do nothing with it.</p> <p>So my sugestion is to add one column to Photo model, on example: <code>photo_type</code>. And set it to 0 if it is first photo, or set it to 1 if it is second photo. In your associations add <code>:conditions =&gt; {:photo_type =&gt; 0}</code> and <code>:conditions =&gt; {:photo_type =&gt; 1}</code>, respectively. I know it is not a solution you are looking for, but I can't find anything better. By the way, maybe it would be better to just use <code>has_many</code> association?</p>
    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.
 

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