Sql 选择RubyonRails的最低价格
我的视图索引中有这段代码,它显示了我的物品的价格Sql 选择RubyonRails的最低价格,sql,ruby-on-rails,Sql,Ruby On Rails,我的视图索引中有这段代码,它显示了我的物品的价格 <% for import_price in ItemImportPrice.find(:all, :conditions => ['itemCode = ? and beginDate = ?', item.short_name, item_days.day) ], :order => ['price asc'])
<% for import_price in ItemImportPrice.find(:all,
:conditions => ['itemCode = ? and beginDate = ?', item.short_name, item_days.day) ],
:order => ['price asc']) %>
<%= import_price.price %>
<%= import_price.superItemType %>
...
<% end %>
如何做到这一点将显示superItemType(I、O、B、D)的最低价格
如果我添加:group=>“supersateroomtype”,它会显示4个项目,但价格不是最低的
顺便说一句,我可能会采用这种方法,但我不知道如何将其应用于所有
ItemImportPrice.all(:select => "Min(price) as min_price", :conditions => ["itemCode = ? and beginDate = ?", item.short_name, item_days.day]).first.min_price
请试一下
ItemImportPrice.select('MIN(price) AS min_price, superItemType').group('superItemType')
您可以这样使用它:
ItemImportPrice.all(:select=>“superItemType,Min(price)as Min_price”,:conditions=>[“itemCode=?and beginDate=?”,item.short_name,item_days.day],:group=>“superItemType”).first.min_price请查看rails中最小函数的文档为什么要将所有这些逻辑放在视图中?因为item.short_name和item_days.day仅在视图中定义此方法错误“private method`select'called for#”p.s.rails 2.3 ruby 1.8.7
ItemImportPrice.select('MIN(price) AS min_price, superItemType').group('superItemType')