title: ActiveRecord has_many through polymorphic has_many
body: <p>It seems like rails still not support this type of relation and throws ActiveRecord::HasManyThroughAssociationPolymorphicThroughError error. </p>
  
  <p>What can I do to implement this kind of relation?</p>
  
  <p>I have following associations:</p>
  
  <pre><code>Users 1..n Articles
  Categories n..n Articles
  Projects 1..n Articles
  </code></pre>
  
  <p>And here is Subscription model</p>
  
  <pre><code>Subscription 1..1 User
  Subscription 1..1 Target (polymorphic (Article, Category or User))
  </code></pre>
  
  <p>And I need to select articles through Subscription#target#article according to user#subscriptions. </p>
  
  <p>I have no idea hot to implement this</p>
  
  <p>Ideally I want to get instance of Association class</p>
  
  <p><strong>UPDATE 1</strong> </p>
  
  <p>Here is little example</p>
  
  <p>Let say user_1 has 4 Subscription records:</p>
  
  <pre><code>s1 = (user_id: 1, target_id: 3, target_type: 'User')
  s2 = (user_id: 1, target_id: 2, target_type: 'Category')
  s3 = (user_id: 1, target_id: 3, target_type: 'Project')
  s4 = (user_id: 1, target_id: 8, target_type: 'Project')
  </code></pre>
  
  <p>I need method User#feed_articles, that fetches all articles, that belong to any of target, I subscribed.</p>
  
  <pre><code>user_1.feed_articles.order(created_at: :desc).limit(10) 
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I separate articles sources by type in User model: </p>
  
  <pre><code>  has_many :out_subscriptions, class_name: 'Subscription'
  
    has_many :followes_users, through: :out_subscriptions, source: :target, source_type: 'User'
    has_many :followes_categories, through: :out_subscriptions, source: :target, source_type: 'Category'
    has_many :followes_projects, through: :out_subscriptions, source: :target, source_type: 'Project'
  
    has_many :feed_user_articles, class_name: 'Article', through: :followes_users, source: :articles
    has_many :feed_category_articles, class_name: 'Article', through: :followes_categories, source: :articles
    has_many :feed_project_articles, class_name: 'Article', through: :followes_projects, source: :articles
  </code></pre>
  
  <p>But how can I merge feed_user_articles with feed_category_articles and feed_project_articles without loss of perfomance</p>
  
  <p><strong>UPDATE 3.1</strong></p>
  
  <p>The only way I found is to use raw SQL join query. Looks like it works fine, but I'm not sure.</p>
  
  <pre><code>  def feed_articles
      join_clause = &lt;&lt;JOIN
  inner join users on articles.user_id = users.id
  inner join articles_categories on articles_categories.article_id = articles.id
  inner join categories on categories.id = articles_categories.category_id
  inner join subscriptions on
      (subscriptions.target_id = users.id and subscriptions.target_type = 'User') or
      (subscriptions.target_id = categories.id and subscriptions.target_type = 'Category')
  JOIN
  
      Article.joins(join_clause).where('subscriptions.user_id' =&gt; id).distinct
    end
  </code></pre>
  
  <p>(This is just for Users and Categories)</p>
  
  <p>It supports scopes and other features. The only thing interests me: does this query lead to some undesirable effect? </p>