# Newbie - Grouping of items

**URL:** <https://www.ruby-forum.com/t/newbie-grouping-of-items/58728>\
**Category:** Rails\
**Created:** [April 25, 2006, 11:21am UTC](https://www.ruby-forum.com/t/newbie-grouping-of-items/58728 "2006-04-25T11:21:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 25, 2006, 11:21am UTC](https://www.ruby-forum.com/t/newbie-grouping-of-items/58728/1 "2006-04-25T11:21:24Z")

</div>

I’m trying to build a dashboard type page (similar in function to  
basecamp’s) where I can show the last 5 updated items from my 3  
differing models, then grouped together by project.

Solution ideas:  
(1) Is it best to create a log model where everytime I add or update  
another model (like messages, comments, lists etc) an enrty gets added  
to the log along with the project. I can see this being an overkill and  
the duplication of data is not correct.

(2) In my controller perform a query that searches every model for every  
item against a given project\_id. The order the results by “updated\_on”.  
Then select the last 5 for each result. This seems really complicated  
and perfoms many SQL queries

Does rails/ruby have a better way of achieving this? What have you done  
in your projects that works reliably and quickly?

Many thanks

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 25, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/newbie-grouping-of-items/58728/2 "2006-04-25T16:11:31Z")

</div>

this is how I would do it:

- add a new array for your Project model, it would be called  
“last\_updates”.

THEN

- create an observer that append the updates to the last\_updates array  
every time there is an update.

OR

-overwrite the ‘after\_save’ (dont remember the exact name of the  
callback) callback from each model that you want to track to add the  
change to the last\_updates array.

finaly, your model should have a method that returns the last X updates  
as an array to the view.

It would also be a good idea to restrict the size of the array. Or  
replace the array with a new DB table to log all the updates…

Anyways, I am new ot RoR so I cant wait to see what others are going to  
say!

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 25, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/newbie-grouping-of-items/58728/3 "2006-04-25T16:44:57Z")

</div>

I think your second approach is best.  
It should only require one find for each model, which probably isn’t  
that bad.

Object.find\_by\_project(project\_id, :order=\>‘updated\_on DESC’, :limit=\>5)  
or more likely…

user.items(:order=\>‘updated\_on DESC’, :conditions=\>“updated\_on \>  
#{Time.now-24.hours}”)

if you have a user has\_many :items, :through=\>:projects

this will give you the most flexibility.

If you create a ‘log’ object, you could use a polymorphic association to  
make it efficient, but you would either have to duplicate information in  
the log table (project\_id, etc) or you would only be able to sort it by  
time

_disclaimer_ - code not tested

On Tuesday, April 25, 2006, at 11:21 AM, James W. wrote:

> (2) In my controller perform a query that searches every model for every  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

\_Kevin

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 25, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/newbie-grouping-of-items/58728/4 "2006-04-25T17:03:50Z")

</div>

Kevin O. wrote:

> I think your second approach is best.  
> It should only require one find for each model, which probably isn’t  
> that bad.
> 
> Object.find\_by\_project(project\_id, :order=\>‘updated\_on DESC’, :limit=\>5)  
> or more likely…
> 
> user.items(:order=\>‘updated\_on DESC’, :conditions=\>“updated\_on \>  
> #{Time.now-24.hours}”)
> 
> if you have a user has\_many :items, :through=\>:projects
> 
> this will give you the most flexibility.
> 
> If you create a ‘log’ object, you could use a polymorphic association to  
> make it efficient, but you would either have to duplicate information in  
> the log table (project\_id, etc) or you would only be able to sort it by  
> time
> 
> _disclaimer_ - code not tested
> 
> On Tuesday, April 25, 2006, at 11:21 AM, James W. wrote:
> 
> > (2) In my controller perform a query that searches every model for every  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> \_Kevin

Thanks all, I have not looked at the through=\> yet sounds promising. I  
have a slight issue with case statements in my view (for my menu items)  
(only on osx see other post) at the moment so I will test soon. Thanks  
for your help.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [May 9, 2006, 11:38am UTC](https://www.ruby-forum.com/t/newbie-grouping-of-items/58728/5 "2006-05-09T11:38:35Z")

</div>

James W. wrote:

> Kevin O. wrote:
> 
> > I think your second approach is best.  
> > It should only require one find for each model, which probably isn’t  
> > that bad.
> > 
> > Object.find\_by\_project(project\_id, :order=\>‘updated\_on DESC’, :limit=\>5)  
> > or more likely…
> > 
> > user.items(:order=\>‘updated\_on DESC’, :conditions=\>“updated\_on \>  
> > #{Time.now-24.hours}”)
> > 
> > if you have a user has\_many :items, :through=\>:projects
> > 
> > this will give you the most flexibility.
> > 
> > If you create a ‘log’ object, you could use a polymorphic association to  
> > make it efficient, but you would either have to duplicate information in  
> > the log table (project\_id, etc) or you would only be able to sort it by  
> > time
> > 
> > _disclaimer_ - code not tested
> > 
> > On Tuesday, April 25, 2006, at 11:21 AM, James W. wrote:
> > 
> > > (2) In my controller perform a query that searches every model for every  
> > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > > 
> > > * * *
> > > 
> > > Rails mailing list  
> > > removed\_email\_address@domain.invalid  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> > 
> > \_Kevin
> 
> Thanks all, I have not looked at the through=\> yet sounds promising. I  
> have a slight issue with case statements in my view (for my menu items)  
> (only on osx see other post) at the moment so I will test soon. Thanks  
> for your help.

Sorry to bring this back to the front, but I still have major problems  
getting this to work. What I am trying to get is like this:

Project 1  
todo item 12 - 03/05/06 12:33  
message 22 - 03/05/06 12:21  
message 66 - 03/05/06 10:55  
todo item 18 - 02/05/06 09:33  
milestone 2 - 02/05/06 09:20  
todo item 4 - 01/05/06 09:20

Project 2  
message 31 - 03/05/06 12:15  
todo item 13 - 02/05/06 11:48  
milestone 28 - 02/05/06 09:20

Project 3  
…

I a client has many projects. A project has many messages, tickets,  
todo\_lists, milestones. A todo\_list has many todo\_list\_items.

I am trying to get a homepage that will group the latest updated items  
(that could be a milestone, message or todo\_list item) by project  
ordered by date.

I have looked at the group\_by Rails 1.1 but that does not achieve what I  
need like above. I can’t think how to best do this can anyone else give  
me an idea?
