title: How do I find() all records unique in certain fields?
body: <p>I have a list of 'request' objects, each of which has fairly normal activerecord qualities.  The requests table is related to the games table with a join table, 'games_requests,' so that a request has a request.games array.</p>
  
  <p>The question is, is there a way to do a find for the last n unique requests, where uniqueness is defined by the games column and a couple others, but specifically ignores other colums (like the name of the requesting user?)</p>
  
  <p>I saw a syntax like 'find (:all, :limit=>5, :include=>[:games,:stage])' but that was returning duplicates.</p>
  
  <p>Thanks...</p>
  
  <p>EDIT: Thanks to chaos for a great response.  You got me really close, but I still need the returns to be valid request objects: the first 5 records that are distinct in the requested rows.  I could just use the find as you constructed it and then do a second find for the first row in the table that matches each of the sets returned by the first find.</p>
  
  <p>EDIT: </p>
  
  <pre><code>Games.find(
      :all, :limit =&gt; 5,
      :include =&gt; [:games, :requests],
      :group =&gt; 'games, whatever, whatever_else'
  )
  </code></pre>
  
  <p>...gives an SQL error:</p>
  
  <pre><code>Mysql::Error: Unknown column 'games' in 'group statement': SELECT * FROM `games`  GROUP BY games
  </code></pre>
  
  <p>I made a few changes for what I assumed to be correct for my project; getting a list of requests instead of games, etc:</p>
  
  <pre><code>Request.find(
      :all, :order=&gt;"id DESC", :limit=&gt;5,
      :include=&gt;[:games],   #including requests here generates an sql error
      :group=&gt;'games, etc'  #mysql error:  games isn't an attribute of requests
      :conditions=&gt;'etc'
  )
  </code></pre>
  
  <p>I'm thinking I'm going to have to use the :join=> option here.</p>