Note that there are some explanatory texts on larger screens.

plurals
  1. POhas_many :through via secondary polymorphism
    text
    copied!<p>Given the associations defined below, I'm hoping someone can shed some light on why I can't access all the Classifieds, that belong to a Category, that in turn belongs to a Section. I'm guessing it has something to do with the fact that there's a polymorphic relationship in there, but I'd like to know if there is a proper way to do what I want using just association statements, or if I have to "roll my own" and get the Classifieds in two-phases. </p> <p>Probably easier to understand by just seeing the code:</p> <pre><code>class CategorySection &lt; ActiveRecord::Base has_many :categories has_many :categorizations, :through =&gt; :categories has_many :classifieds, :through =&gt; :categories end class Category &lt; ActiveRecord::Base belongs_to :section, :class_name =&gt; 'CategorySection', :foreign_key =&gt; 'category_section_id', :counter_cache =&gt; true has_many :categorizations has_many :classifieds, :through =&gt; :categorizations, :source =&gt; :categorizable, :source_type =&gt; 'Classified' end class Categorization &lt; ActiveRecord::Base belongs_to :category, :counter_cache =&gt; true belongs_to :categorizable, :polymorphic =&gt; true end class Classified &lt; ActiveRecord::Base has_one :categorization, :as =&gt; :categorizable, :dependent =&gt; :destroy has_one :category, :through =&gt; :categorization end </code></pre> <p>For the most part, this is all working properly except for one association I can't figure out. Given a CategorySection, how can I quickly find out all the Classifieds that belong to it? </p> <p>For example:</p> <p>Given a Category, I can get all it's categorizations:</p> <pre><code>&gt;&gt; @category.categorizations &gt;&gt; [&lt;Categorization...&gt;,&lt;Categorization...&gt;] </code></pre> <p>Given a Category, I can get all the classifieds in it:</p> <pre><code>&gt;&gt; @category.classifieds &gt;&gt; [&lt;Classified...&gt;,&lt;Classified...&gt;] </code></pre> <p>Given a Section, I can get all of its categories:</p> <pre><code>&gt;&gt; @section.categories &gt;&gt; [&lt;Category...&gt;,&lt;Category...&gt;] </code></pre> <p>Given a Section, I can get all of its categorizations going through :categories</p> <pre><code>&gt;&gt; @section.categorizations &gt;&gt; [&lt;Categorization...&gt;,&lt;Categorization...&gt;] </code></pre> <p>But, given a Section, I cannot get all of its classifieds going through :categories</p> <pre><code>&gt;&gt; @section.classifieds ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_many :through for has_many :classifieds, :through =&gt; :categories. Use :source to specify the source reflection. </code></pre> <p>I've taking the error message's advice of specifying a :source, but I still can't get it to work. I've tried pretty much every combination of options on that association that I can think of but to no avail. </p> <p>Any advice or suggestions would be most appreciated. Thanks in advance.</p> <p>Sincerely, Kenny</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