Fetching Minimum/Maximum for each group in ActiveRecord

前端 未结 5 1276
说谎
说谎 2021-02-05 20:53

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.

5条回答
  •  死守一世寂寞
    2021-02-05 21:44

    Table.minimum(:price, :group => :type)
    

    See http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-minimum for more.

提交回复
热议问题