title: Complex SQL query across habtm join table with ordering in rails
body: <p>I have three tables:</p>
  
  <pre><code>Posts
  Keywordings
  Keywords
  </code></pre>
  
  <p>Relevant fields in parens.</p>
  
  <p>A Post</p>
  
  <pre><code>has_many :keywordings
  has_many :keywords, :through =&gt; :keywordings
  </code></pre>
  
  <p>A Keywording(post_id, keyword_id)</p>
  
  <pre><code>belongs_to :post
  belongs_to :keyword
  </code></pre>
  
  <p>A Keyword(name)</p>
  
  <pre><code>has_many :keywordings
  has_many :posts, :through =&gt; :keywordings
  </code></pre>
  
  <p>I want to find all posts that have keywords that match any (by name) from a list, ordered by how many keywords got matched. I'm convinced this can all be done in SQL, but I'm at a loss.</p>
  
  <p>If I have to do somethings in Ruby, fine, but preferably all in SQL. It does have to be fast.</p>