How to define new instantaneous variable row by row - RAILS3 BEGINNER
I was hoping somebody may be able to point me in the right direction...
I have a database called Info and use a find command to select the rows in this database which match a certain criteria
@matching = Info.find( :all, :conditions => ["product_name = ?", distinctproduct], :order => 'Price ASC')
I then pull out the cheapest of these items
@cheapest = @matching.first
Finally, I would like to create an instantaneous开发者_运维百科 array which contains a list of @cheapest for a number of different search criteria. i.e. row 1 in @allcheapest is @cheapest for criteria 1, row 2 in @allcheapest is @cheapest for criteria 2, ...
Any help would be great, thanks in advance
Info.where(:product_name => distinct_product.to_s).order('Price ASC').first
to select the cheapest price for the product_name. Without more insight into how your database is structured, it is difficult to suggest how to obtain the latter, but you may try
Info.where(:product_name => distinct_product.to_s).order('Price ASC').group(:product_name)
精彩评论