# .maximum to return a full row not just one value

**URL:** <https://www.ruby-forum.com/t/maximum-to-return-a-full-row-not-just-one-value/126462>\
**Category:** Rails\
**Created:** [January 18, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/maximum-to-return-a-full-row-not-just-one-value/126462 "2008-01-18T13:37:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [January 18, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/maximum-to-return-a-full-row-not-just-one-value/126462/1 "2008-01-18T13:37:52Z")

</div>

Hi,

I have a Rating table in my SQL database and I would like to return the  
row containing the highest average rating where the picture\_id for that  
row is a certain value. I could do this by SQL but I’d like to use the  
Ruby functions if it’s possible.

I can get the maximum value of rating like this:

@rating = Rating.maximum(:rating, :conditions =\> [‘picture\_id = ?’,  
@picture\_id])

which works great but only returns the maximum value for the column  
“rating” when I want the whole row of values for the record with the  
maximum rating.

The SQL would be something like this:

SELECT user\_id, picture\_id, max(rating) FROM ratings WHERE picture\_id =  
? GROUP BY user\_id, picture\_id’, @picture\_id]

I’ve not tested that but you get the idea…

So is there anyway of running the above SQL using a ruby function ?

Thanks for any help

Phil
