Complex ARel query <p>I've got a complicated query that I can't wrap my head around (using either sql or ActiveRecord) Here are my models:</p> <pre><code>class Contact has_many :profile_answers end class ProfileAnswer belongs_to :contact belongs_to :profile_question end class ProfileQuestion has_many :profile_answers end </code></pre> <p>I'm trying to find the number of ProfileAnswers for two contacts that have the same <code>value</code> for a particular ProfileQuestion. In other words:</p> <blockquote> <p>Get the total number of profile answers that two contacts have answered with the same value for a particular profile_question</p> </blockquote> <p>I don't want to make multiple queries and filter as I know this is possible with Sql only, i just don't know how to do it</p> <p>I had considered a self join of <code>profile_answers</code> on <code>profile_question_id</code> then filtering by <code>value</code> being equal, but i still can't wrap my head around that. Any help is greatly appreciated.</p>