Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I get this right, you not only want to load a <strong>polymorphic association's association</strong>, but a <strong>polymorphic association's <em>polymorphic</em> association</strong>.</p> <p>This basically means joining one defined table with another defined table through a bunch of undefined tables that come from some fields in the database.</p> <p>Since both the activity and the post are joined through a polymorphic object they both have a <code>something_id</code> and <code>something_type</code> column.</p> <p>Now I think active record doesn't let you do this out of the box but basically you want something like:</p> <pre><code>class Activity has_one :post, :primary_key =&gt; [:link_id, :link_type], :foreign_key =&gt; [:postable_id, :postable_type] end </code></pre> <p>(assuming your polymorphic association on <code>Post</code> is <code>belongs_to :postable</code>)</p> <p>That would then give you a query sort-of direct association between <code>Post</code> and <code>Activity</code> that is a very weird take on <code>habtm</code> with a 'polymorphic join table' (I just made that term up). Because both share the same polymorphicly associated object, they can be connected. Sort of like a friends-of-my-friends thing.</p> <p><strong>CAVEAT</strong>: Like I said, as far as I know, AR doesn't let you do this out of the box (though it would be awesome) but there are some gems that give you composite foreign and/or primary keys. Maybe you can find one to help you solve your problem in this way.</p>
 

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