title: Search scope for contains multiple tags
body: <p>For the purpose of this question, let's assume I'm building Stack Overflow. </p>
  
  <p>I've got a many-to-many relationship via an association table for <code>Posts</code> and <code>Tags</code>, and I'd like to search for posts that contain multiple tags. </p>
  
  <p>I can search for posts that contain a single tag like this:</p>
  
  <pre><code>scope :has_tag, lambda { |tag| includes(:tags).where("lower(tags.name) = ?", tag.downcase) }
  
  Post.has_tag("Python")
  </code></pre>
  
  <p>For searching for multiple tags, I've tried building up a query using a loop:</p>
  
  <pre><code>def self.has_tags(tags)
    query = includes(:tags)
    tags.each do |tag|
      query = query.where("lower(tags.name) = ?", tag)
    end
    return query
  end
  </code></pre>
  
  <p>This results in the query <code>... WHERE (lower(tags.name) = 'tag1') AND (lower(tags.name) = 'tag2')</code>, which doesn't match anything. </p>
  
  <p>I've also tried using <code>.where('tags.name IN (?)', tags)</code>, but I think that matches any post that contains one of the tags instead of posts that contain all the tags. </p>
  
  <p>Now I'm thinking that perhaps I should use the ID of the tags, but that sounds like it could result in multiple queries. Is this inefficient?</p>
  
  <pre><code>... WHERE tag_id IN (SELECT id FROM tag WHERE name IN ('tag1','tag2'))
  </code></pre>
  
  <hr>
  
  <p>What's the best way to search for multiple tags?<br>
  (I'd prefer to understand the syntax and do it manually rather than using a gem, since I'm new to Rails.)</p>