title: column "users.id" must appear in the GROUP BY clause or be used in an aggregate function
body: <p>Relationships:</p>
  
  <pre><code>Item belongs to Product
  Product belongs to User
  </code></pre>
  
  <p>Item model scope:</p>
  
  <pre><code>  scope :search, -&gt;(search_term) {
      select('products.name, users.*, products.brand, COUNT(products.id)')
      .joins(:product =&gt; :user)
      .where('users.name = ? OR products.brand = ?', search_term, search_term)
      .group('products.id')
    }
  </code></pre>
  
  <p>The above results in the following SQL statement:</p>
  
  <pre><code>SELECT products.name, users.*, products.brand, COUNT(products.id) FROM "items" 
  INNER JOIN "products" ON "products"."id" = "items"."product_id" 
  INNER JOIN "users" ON "users"."id" = "products"."user_id" 
  WHERE (users.name = 'Atsuete Lipstick' OR products.brand = 'Atsuete Lipstick') 
  GROUP BY products.id
  </code></pre>
  
  <p>The problem here is that an error occurs:</p>
  
  <pre><code>ActiveRecord::StatementInvalid: PG::Error: ERROR:  column "users.id" 
  must appear in the GROUP BY clause or be used in an aggregate function
  LINE 1: SELECT products.name, users.*, products.brand, COUNT(product...
  </code></pre>
  
  <p>What could be a fix for this?</p>