title: Rails 4: combining has_many :through association with polymorphic association
body: <p>In my Rails 4 app, I have the following models:</p>
  
  <pre><code>User
  has_many :administrations
  has_many :calendars, through: :administrations
  has_many :comments
  has_many :calendar_comments, through: :calendars, :source =&gt; :comments
  
  Calendar
  has_many :administrations
  has_many :users, through: :administrations
  has_many :posts
  has_many :comments, through: posts
  has_many :ads
  
  Administration
  belongs_to :user
  belongs_to :calendar
  
  Post
  belongs_to :calendar
  has_many :comments, as: :commentable
  
  Ad
  belongs_to :calendar
  has_many :comments, as: :commentable
  
  Comment
  belongs_to :commentable, polymorphic: true
  belongs_to :user
  </code></pre>
  
  <p>I need to access the <code>comments</code> that <code>belong_to</code> an <code>ad</code> from the <code>calendar</code> the <code>ad</code> <code>belongs_to</code>.</p>
  
  <p>This is what I am trying to do in my <code>Calendars#Index</code> action:</p>
  
  <pre><code>@posts_comments = @user.calendar_comments.where(commentable_type: "Post").order("created_at DESC").limit(5)
  @ads_comments = @user.calendar_comments.where(commentable_type: "Ad").order("created_at DESC").limit(5)
  </code></pre>
  
  <p>My first guess was to add <code>has_many :comments, through: ads</code> in the Calendar model:</p>
  
  <pre><code>Calendar
  has_many :administrations
  has_many :users, through: :administrations
  has_many :posts
  has_many :comments, through: posts
  has_many : ads
  has_many :comments, through: ads
  </code></pre>
  
  <p>But that cancels the effect of <code>has_many :comments, through: posts</code> and then I can no longer access the <code>comments</code> that <code>belong_to</code> an <code>post</code> from the <code>calendar</code> the <code>post</code> <code>belongs_to</code>.</p>
  
  <p>Is there a way to make <strong>BOTH</strong> <code>has_many :comments, through: posts</code> <strong>AND</strong> <code>has_many :comments, through: ads</code> work?</p>
  
  <p>—————</p>
  
  <p><strong>UPDATE</strong>: according to <a href="http://www.informit.com/articles/article.aspx?p=2220311&amp;seqNum=6" rel="nofollow noreferrer">this article</a> and <a href="https://stackoverflow.com/questions/5886738/setting-up-a-polymorphic-has-many-through-relationship">that Stack Overflow question</a>, the answer may lay in the use of <code>source:</code> and <code>source_type:</code>.</p>
  
  <p>Not sure how to use those however.</p>
  
  <p>—————</p>
  
  <p><strong>UPDATE 2</strong>: would the following code make any sense?</p>
  
  <pre><code>class Calendar &lt; ActiveRecord::Base
    has_many :administrations, dependent: :destroy
    has_many :users, through: :administrations
    has_many :posts, dependent: :destroy
    has_many :commented_posts, through: :comments, source: :commentable, source_type: 'Post'
    has_many :ads, dependent: :destroy
    has_many :commented_ads, through: :comments, source: :commentable, source_type: 'Ad'
  </code></pre>
  
  <p>—————</p>
  
  <p><strong>UPDATE 3</strong>: when I try the above code, I get the following error message:</p>
  
  <pre><code>Could not find the source association(s) :comments in model Calendar. Try 'has_many :calendar_comments, :through =&gt; :calendars, :source =&gt; &lt;name&gt;'. Is it one of administrations, users, posts, commented_posts, ads, commented_ads, invites, or pokes?
  </code></pre>
  
  <p>I tried the following:</p>
  
  <pre><code>class Calendar &lt; ActiveRecord::Base
    has_many :administrations, dependent: :destroy
    has_many :users, through: :administrations
    has_many :posts, dependent: :destroy
    has_many :calendar_comments, :through =&gt; :calendars, :source =&gt; :post
    has_many :ads, dependent: :destroy
    has_many :calendar_comments, :through =&gt; :calendars, :source =&gt; :ad
  </code></pre>
  
  <p>—————</p>
  
  <p><strong>UPDATE 4</strong>: a new failed attempt with the following code:</p>
  
  <pre><code>class Calendar &lt; ActiveRecord::Base
    has_many :administrations, dependent: :destroy
    has_many :users, through: :administrations
    has_many :posts, dependent: :destroy
    has_many :calendar_comments, :through =&gt; :commentable, :source =&gt; :post
    has_many :ads, dependent: :destroy
    has_many :calendar_comments, :through =&gt; :commentable, :source =&gt; :ad
  </code></pre>
  
  <p>Still not working, same error message as above.</p>
  
  <p>—————</p>
  
  <p><strong>UPDATE 5</strong>: based on <strong>MrYoshiji</strong>'s answer, I now have</p>
  
  <pre><code>class Calendar &lt; ActiveRecord::Base
    has_many :administrations, dependent: :destroy
    has_many :users, through: :administrations
    has_many :posts, dependent: :destroy
    has_many :posts_comments, through: :posts, source_type: 'Post'
    has_many :ads, dependent: :destroy
    has_many :ads_comments, through: :ads, source_type: 'Ad'
  </code></pre>
  
  <p>Now, <code>has_many :calendar_comments, through: :calendars, :source =&gt; :comments</code> in the <code>User</code> model is no longer working.</p>
  
  <p>I tried:</p>
  
  <pre><code>has_many :comments
  has_many :calendar_post_comments, through: :calendars, :source =&gt; :post_comments
  has_many :calendar_ad_comments, through: :calendars, :source =&gt; :ad_comments
  </code></pre>
  
  <p>Still not working.</p>
  
  <p>—————</p>
  
  <p><strong>UPDATE 6</strong>: I am still stuck with this issue, since I cannot figure out a way to get the following code working:</p>
  
  <pre><code>@posts_comments = @user.calendar_comments.where(commentable_type: "Post").order("created_at DESC").limit(5)
  @ads_comments = @user.calendar_comments.where(commentable_type: "Ad").order("created_at DESC").limit(5)
  </code></pre>
  
  <p>I tried many different things, and the best I could come up with so far was:</p>
  
  <pre><code>class Calendar &lt; ActiveRecord::Base
    has_many :administrations, dependent: :destroy
    has_many :users, through: :administrations
    has_many :posts, dependent: :destroy
    has_many :post_comments, through: :posts, source_type: 'Post'
    has_many :ads, dependent: :destroy
    has_many :ad_comments, through: :ads, source_type: 'Ad'
  end
  
  class User &lt; ActiveRecord::Base
    has_many :administrations, dependent: :destroy
    has_many :calendars, through: :administrations
    has_many :comments
    has_many :calendar_post_comments, through: :calendars, :source =&gt; :post_comments
    has_many :calendar_ad_comments, through: :calendars, :source =&gt; :ad_comments
  end
  </code></pre>
  
  <p>Then, I update my Calendars controller as follows:</p>
  
  <pre><code>def index
      @user = current_user
      @calendars = @user.calendars.all
      @posts_comments = @user.calendar_post_comments
                                .order("created_at DESC")
                                .limit(5)
      @ads_comments = @user.calendar_ad.comments
                                .order("created_at DESC")
                                .limit(5)
  end
  </code></pre>
  
  <p>But this returns the following error:</p>
  
  <pre><code>NoMethodError at /calendars
  undefined method `chain' for nil:NilClass
  @posts_comments = @user.calendar_post_comments.order("created_at DESC").limit(5)
  </code></pre>
  
  <p>What is wrong here?</p>