title: PostgreSQL, Rails + Heroku, Column must appear in "group by"
body: <p>I'm getting this error when I deploy my app on Heroku:</p>
  
  <pre><code>Started GET "/collections/transect/search?utf8=%E2%9C%93&amp;search%5Btagged_with%5D=village&amp;commit=Search" for 98.201.59.6 at 2011-03-27 17:02:12 -0700
  
  ActionView::Template::Error (PGError: ERROR:  column "photos.custom_title" must appear in the GROUP BY clause or be used in an aggregate function
  : SELECT  "photos".* FROM "photos" INNER JOIN "taggings" ON "photos"."id" = "taggings"."photo_id" INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" WHERE "tags"."name" IN ('village') AND ("photos".collection_id = 1) GROUP BY photos.id LIMIT 20 OFFSET 0):
       17: 
       18: - @bodyclass = 'dark'
       19: #search_view.photo_tiles
       20:   = render :partial =&gt; 'collections/photos/alt_tiles', :collection =&gt; @photos, :as =&gt; :photo
     app/views/collections/search.html.haml:20:in `_app_views_collections_search_html_haml__2343730670144375006_16241280__2249843891577483539'
  </code></pre>
  
  <p>I saw these similar questions (<a href="https://stackoverflow.com/questions/4599010/heroku-postgresql-group-by-error-in-rails-app">1</a>,<a href="https://stackoverflow.com/questions/2686055/error-when-pushing-to-heroku-appear-in-group-ruby-on-rails">2</a>). </p>
  
  <p>The problem is, nothing in this view is asking for the custom_title attribute, nor am I executing a query with a "group_by" clause.</p>
  
  <p>Here's the partial that seems to trigger the error:</p>
  
  <pre><code>- ((photo_counter+1) % 5 == 0) ? @class = 'last' : @class = ''
  .photo{ :class =&gt; @class }
    .alt_tile
      = link_to( image_tag(photo.file.url(:tile)), collection_photo_path(@collection,photo), :class =&gt; 'img_container' )
      .location= photo.location(:min)
      .tags= photo.tag_array.join(' | ')
  </code></pre>
  
  <p>Here's the <code>collections#search</code> action which is what raised the error:</p>
  
  <pre><code>def search
    @curator_toolbar = true
    @collection = Collection.find(params[:id])
    @search = @collection.photos.search(params[:search])
    @photos = @search.page(params[:page]).per(20)
  end
  </code></pre>
  
  <p>So it looks like maybe this is a plugin issue? I'm using MetaSearch for search functionality and Kaminari for pagination. Does anyone have any ideas or suggestions as to what would cause this specifically and how I can possibly fix it?</p>
  
  <p>--EDIT--</p>
  
  <p>Ok, I seem to have found the real problem:</p>
  
  <p>Using MetaSearch with my keyword tags model, I created a search method that looks like this:</p>
  
  <pre><code>def self.tagged_with( string )
    array = string.split(',').map{ |s| s.lstrip }
    joins(:tags).where('tags.name' =&gt; array ).group('photos.id')
  end
  </code></pre>
  
  <p>Now, I was given a lot of help in creating this method -- as I mentioned before I'm a total SQL moron.</p>
  
  <p>This method works on SQLite but not on PostgreSQL because whenever keywords are included in a search it triggers the "group_by" problem.</p>
  
  <p>So, in <a href="https://stackoverflow.com/questions/2699704/adding-table-columns-to-a-group-by-clause-ruby-on-rails-postgresql">this question</a> it seems to indicate that I need to put every column that is part of my photo model in the "group" argument or Postgre will break.</p>
  
  <p>That horrifies me for several reasons:</p>
  
  <ol>
  <li>My photo model is pretty complex and has a ton of fields.</li>
  <li>My app is still in development and the photo model changes more than any other.</li>
  <li>I don't want to have my code breaking every time someone touches the photo model in the future if they forget to add the columns to the group statement on the tag searching argument.</li>
  </ol>
  
  <p>So, can anyone help me understand how to rewrite this method so that it won't break PostgreSQL -- and ideally so that I won't have to include a list of all the fields that belong to this model in the solution, or at least not a manually maintained list?</p>