Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择RubyonRails的最低价格_Sql_Ruby On Rails - Fatal编程技术网

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')