# Grouping. Difficult find statement. Brain hurting

**URL:** <https://www.ruby-forum.com/t/grouping-difficult-find-statement-brain-hurting/81626>\
**Category:** Rails\
**Created:** [January 10, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/grouping-difficult-find-statement-brain-hurting/81626 "2007-01-10T20:08:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [January 10, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/grouping-difficult-find-statement-brain-hurting/81626/1 "2007-01-10T20:08:13Z")

</div>

Hey guys,

I’m sure this is very simple but i just can’t figure it out.

I have profiles and each time a user visits a profile, the visit is  
recorded in the following model…

**profile\_views**  
id  
user\_id #the id of the user who is visiting the profile  
profile\_user\_id #the id of the user who is being visited  
viewed\_at

This is what I am trying to do (in sudo code)

Find all the visitors for a particular profile only returning each  
visitor once and order the whole list by viewed\_at

This would be an example list…

Mike, 1 min ago  
Scott, 2 mins ago  
Andy, 3 mins ago

etc etc

This is what I have do far…

@latest\_visitors = ProfileView.find(:all, :conditions =\>  
[“profile\_user\_id = ?”, params[:id]], :order =\> ‘viewed\_at DESC’, :limit  
=\> 5, :group =\> ‘user\_id’)

This returns something along the line of what I want - it returns only 1  
record for each user, but it returns the very first time that user  
visited the profile.

Any ideas what I’m doing wrong?

Thank you!!

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [January 10, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/grouping-difficult-find-statement-brain-hurting/81626/2 "2007-01-10T20:29:14Z")

</div>

Try this:

@latest\_visitors = ProfileView.find(:all,  
:conditions =\> [“profile\_user\_id = ?”, params[:id]],  
:select =\> ‘id, user\_id, profile\_user\_id, max(viewed\_at) viewed\_at’,  
:limit =\> 5, :group =\> ‘user\_id’)

You’ll have to include any other fields you need in the “select” option.

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [January 10, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/grouping-difficult-find-statement-brain-hurting/81626/3 "2007-01-10T20:47:59Z")

</div>

Snowman wrote:

> Try this:
> 
> @latest\_visitors = ProfileView.find(:all,  
> :conditions =\> [“profile\_user\_id = ?”, params[:id]],  
> :select =\> ‘id, user\_id, profile\_user\_id, max(viewed\_at) viewed\_at’,  
> :limit =\> 5, :group =\> ‘user\_id’)
> 
> You’ll have to include any other fields you need in the “select” option.

That worked a treat! thank you!!

I added an ‘:order’ clause to it and it does everything i want.

Was it ok to add the ‘:order’ or is this bad form?

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [January 10, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/grouping-difficult-find-statement-brain-hurting/81626/4 "2007-01-10T20:48:41Z")

</div>

@latest\_visitors = ProfileView.find(:all,  
:conditions =\> [“profile\_user\_id = ?”, params[:id]],  
:select =\> ‘id, user\_id, profile\_user\_id, max(viewed\_at)  
viewed\_at’,  
:limit =\> 5, :group =\> ‘user\_id’, :order =\> “viewed\_at DESC”)

oops. Meant to add your updated code

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [January 10, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/grouping-difficult-find-statement-brain-hurting/81626/5 "2007-01-10T20:52:31Z")

</div>

> Was it ok to add the ‘:order’ or is this bad form?

Can’t see any reason not to, if it does what you need. I should have  
left it in but I didn’t pay close enough attention to the description of  
what you were trying to achieve.
