<p>This is an age-old question where given a table with attributes 'type', 'variety' and 'price', that you fetch the record with the minimum price for each type there is.</p>

<p>In SQL, we can do <a href="http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/" rel="noreferrer">this</a> by:</p>

<pre><code>select f.type, f.variety, f.price   
from (  select type, min(price) as minprice from table group by type ) as x  
inner join table as f on f.type = x.type and f.price = x.minprice;`
</code></pre>

<p>We could perhaps imitate this by:</p>

<pre><code>minprices = Table.minimum(:price, :group =&gt; type)  
result = []
minprices.each_pair do |t, p|  
   result &lt;&lt; Table.find(:first, :conditions =&gt; ["type = ? and price = ?", t, p])
end
</code></pre>

<p>Is there a better implementation than this?</p>

```
```
Table.minimum(:price, :group => :type)
```
```
See
http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-minimum
for more.