Fetching Minimum/Maximum for each group in ActiveRecord

前端 未结 5 1282
说谎
说谎 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:38

    To update Avdi's answer above:

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

    Here is the updated URL:

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

提交回复
热议问题