Rails: Selecting all records that are unique by a specific column
<p>I have a Recommendation model and I would like to select all recommendations that are distinct by their movie_id column. I've tried a few methods but none that have worked yet:</p>

<pre><code>scope :not_duplicate, -&gt; {
    group_by(&amp;:movie_id)
  }

==&gt; doesn't work on a class

scope :not_duplicate, -&gt; {
        uniq_by(&amp;:movie_id)
      }

==&gt; doesn't work on a class

scope :not_duplicate, -&gt; {
        select(:movie_id).group(:movie_id, "recommendations.id")
      }

==&gt; only returns movie_id values and I need entire record

scope :not_duplicate, -&gt; {
        select("recommendations.*").group(:movie_id)
      }

==&gt; PG::GroupingError: ERROR:  column "recommendations.id" must appear in the GROUP BY clause or be used in an aggregate function
</code></pre>

<p>I'm sure this is a pretty regular occurrence but for some reason I can't find the solution. Any ideas?</p>