title: Polymorphic associations break in rails 6.1
body: <p>I have inherited some legacy rails code with workarounds around standard polymorphic associations. These used to work up until rails 5. Now I am trying to upgrade to rails6.1 and they break and I have no clue as to how to fix them.</p>
  <p><strong>Problem:</strong>
  We have following 2 tables:</p>
  <ol>
  <li><p>posts</p>
  <ul>
  <li>id</li>
  <li>text</li>
  <li>...</li>
  </ul>
  </li>
  <li><p>comments</p>
  <ul>
  <li>id</li>
  <li>commentable_id</li>
  <li>commentable_type</li>
  <li>...</li>
  </ul>
  </li>
  </ol>
  <p>A post has 3 types of comments -</p>
  <ol>
  <li>Admin comment</li>
  <li>Anonymous comment</li>
  <li>User comment</li>
  </ol>
  <p>We have modeled this as polymorphic relationship as follows:</p>
  <pre><code># comment.rb
  class Comment &lt; ActiveRecord::Base
    belongs_to :commentable, polymorphic: true
  end
  </code></pre>
  <pre><code># post.rb
  class Post &lt; ActiveRecord::Base
    has_many :comments, as :commentable, dependent: :destroy, inverse_of: :post
  
    has_many :admin_comments, -&gt; { where commentable_type: &quot;AdminComment&quot; },
      class_name: 'Comment', foreign_key: :commentable_id,
      foreign_type: :commentable_type, dependent: :destroy, inverse_of: :post
  
    has_many :user_comments, -&gt; { where commentable_type: &quot;UserComment&quot; },
      class_name: 'Comment', foreign_key: :commentable_id,
      foreign_type: :commentable_type, dependent: :destroy, inverse_of: :post
  
    has_many :anonymous_comments, -&gt; { where commentable_type: &quot;AnonymousComment&quot; },
      class_name: 'Comment', foreign_key: :commentable_id,
      foreign_type: :commentable_type, dependent: :destroy, inverse_of: :post
  end
  </code></pre>
  <p>With this setup, I could fetch either all comments by doing -</p>
  <pre><code>p = Post.find(123)
  p.comments
  </code></pre>
  <p>or fetch comments by specific type of user -</p>
  <pre><code>p = Post.find(123)
  p.user_comments
  p.anonymous_comments
  </code></pre>
  <p>With rails 6.1 this setup does not work.
  When trying to run <code>rails console</code> It fails with error:</p>
  <blockquote>
  <p>ruby2.7.x/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/core_ext/hash/keys.rb:52:in `block in assert_valid_keys': Unknown key: :foreign_type. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :before_add, :after_add, :before_remove, :after_remove, :extend, :counter_cache, :join_table, :index_errors, :ensuring_owner_was (ArgumentError)</p>
  </blockquote>
  <p>After looking for potential answers online, the closest I could get was by adding <code>as: :commentable</code> to each association like -</p>
  <pre><code>  has_many :user_comments, -&gt; { where commentable_type: &quot;UserComment&quot; },
      class_name: 'Comment', foreign_key: :commentable_id,
      foreign_type: :commentable_type, dependent: :destroy, inverse_of: :post, as: :commentable
  </code></pre>
  <p>But this does not get me correct result set. It generates sql as follows -</p>
  <blockquote>
  <p>SELECT comments.* FROM comments WHERE comments.commentable_id = 123 AND comments.commentable_type = 'Post' AND comments.commentable_type = 'UserComment'</p>
  </blockquote>
  <p>without <code>as: :commentable</code> it would generate sql as -</p>
  <blockquote>
  <p>SELECT comments.* FROM comments WHERE comments.commentable_id = 123  AND comments.commentable_type = 'UserComment'</p>
  </blockquote>
  <p>I am kinda lost and would appreciate any help!</p>
  <p>Thanks!</p>