class User < ApplicationRecord has_many :comments end class Article < ApplicationRecord belongs_to :user has_many :comments end class Comment < ApplicationRecord belongs_to :user belongs_to :article end item_type が "Article" の Comment を持つユーザーを取得したい場合、 joins と where を使うと以下のようになります。
![【ActiveRecord】JOIN したテーブルに条件を指定するときは merge を使おう【Tips】 - Qiita](https://cdn-ak-scissors.b.st-hatena.com/image/square/4305a0c3d04f891413de6cd9e3c57b19e589a4d4/height=288;version=1;width=512/https%3A%2F%2Fqiita-user-contents.imgix.net%2Fhttps%253A%252F%252Fcdn.qiita.com%252Fassets%252Fpublic%252Farticle-ogp-background-9f5428127621718a910c8b63951390ad.png%3Fixlib%3Drb-4.0.0%26w%3D1200%26mark64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZ3PTkxNiZoPTMzNiZ0eHQ9JUUzJTgwJTkwQWN0aXZlUmVjb3JkJUUzJTgwJTkxSk9JTiUyMCVFMyU4MSU5NyVFMyU4MSU5RiVFMyU4MyU4NiVFMyU4MyVCQyVFMyU4MyU5NiVFMyU4MyVBQiVFMyU4MSVBQiVFNiU5RCVBMSVFNCVCQiVCNiVFMyU4MiU5MiVFNiU4QyU4NyVFNSVBRSU5QSVFMyU4MSU5OSVFMyU4MiU4QiVFMyU4MSVBOCVFMyU4MSU4RCVFMyU4MSVBRiUyMG1lcmdlJTIwJUUzJTgyJTkyJUU0JUJEJUJGJUUzJTgxJThBJUUzJTgxJTg2JUUzJTgwJTkwVGlwcyVFMyU4MCU5MSZ0eHQtY29sb3I9JTIzMjEyMTIxJnR4dC1mb250PUhpcmFnaW5vJTIwU2FucyUyMFc2JnR4dC1zaXplPTU2JnR4dC1jbGlwPWVsbGlwc2lzJnR4dC1hbGlnbj1sZWZ0JTJDdG9wJnM9MjhiYTgxYTRjYTlhMjhlNWZkNWU4NWJiNWJjNDc1OWU%26mark-x%3D142%26mark-y%3D112%26blend64%3DaHR0cHM6Ly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