title: Rails SQL Where Count
body: <p>I have a Posts Table and A Sec_photo table :</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    has_many :sec_photos
  </code></pre>
  
  <p>I am trying to do an advanced search form where it finds <code>posts</code> based on their sum of <code>sec_photos</code> :
  <code>@posts = @posts.where(:sec_photos.count &gt; 2)</code> is failing and I have looked online and attempted many other solutions but none seem to  work.</p>
  
  <p>Does anyone have any ideas? </p>
  
  <p><em>Ps:</em> It's a necessity for the line to be in the form <code>@posts = @posts.where</code> as that coincides with other conditions. </p>
  
  <p>The advanced search from searches for other fields like category_id and location_id as such </p>
  
  <pre><code>@posts = Post.all
       @posts = @posts.where('category_id = ?', @advanced_search.category_search) if @advanced_search.category_search.present?
       @posts = @posts.where('location_id = ?', @advanced_search.location_search) if @advanced_search.location_search.present?
       @posts = @posts.where("strftime('%m %Y', created_at) = ?", @advanced_search.newdate_search.strftime('%m %Y')) if @advanced_search.newdate_search.present?
  </code></pre>
  
  <p>The last option would be to show posts with sec_photos either more than 2 or less than 2 </p>