title: Rails: Find all records matching condition for any of the associated objects
body: <p>I have the following Ruby on Rails entity:</p>
  
  <p>Playlist:</p>
  
  <pre><code>class Playlist &lt; ActiveRecord::Base {
                               :id =&gt; :integer,
                             :name =&gt; :string,
                       :created_at =&gt; :datetime,
                       :updated_at =&gt; :datetime,
                        :dimension =&gt; :string,
                            :title =&gt; :string,
                             :text =&gt; :string,
                            :price =&gt; :float,
         :playlist_image_file_name =&gt; :string,
      :playlist_image_content_type =&gt; :string,
         :playlist_image_file_size =&gt; :integer,
        :playlist_image_updated_at =&gt; :datetime,
                             :main =&gt; :boolean,
                           :action =&gt; :string,
                   :hairdresser_id =&gt; :integer
  }
  </code></pre>
  
  <p>And Keyword:</p>
  
  <pre><code>class Keyword &lt; ActiveRecord::Base {
               :id =&gt; :integer,
             :name =&gt; :string,
       :created_at =&gt; :datetime,
       :updated_at =&gt; :datetime,
      :preselected =&gt; :boolean
  }
  </code></pre>
  
  <p>The relation between them is very simple: basically a Playlist object can have 0 or more keywords associated.
  Those are the models:</p>
  
  <pre><code>class Keyword &lt; ActiveRecord::Base
    has_and_belongs_to_many :playlists
  end
  
  class Playlist &lt; ActiveRecord::Base
    has_and_belongs_to_many :keywords
  
    has_attached_file :playlist_image, styles: {medium: "500x500", small: "200x200", thumb: "40x40"}, default_url: "/system/playlist_default.jpg"
    validates_attachment_content_type :playlist_image, content_type: /\Aimage\/.*\Z/
  end
  </code></pre>
  
  <p>That allows me to do something like that:</p>
  
  <pre><code>Playlist.first.keywords
  </code></pre>
  
  <p>and retrieve all the keywords associated with the first playlist.</p>
  
  <p>Now I would like to build a function to return all the playlists that have certain words as keywords and have "main" equals to true.
  For example all the playlist that have the keyword "Summer".
  I tryed with that:</p>
  
  <pre><code>Playlist.where(:main =&gt; true).map{|x| x.keywords.include? "Summer"}
  </code></pre>
  
  <p>But that returns only an array containing true or false depending if the related Playlist contains or not the keyword "Summer", I'm looking for something that return the whole playlist only if the array of keywords of that playlist include the word "summer". How can I achieve that?</p>