Forum: Ruby on Rails Newbie - Grouping of items...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-04-25 11:21
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
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain (Guest)
on 2006-04-25 16:11
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!
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-25 16:44
(Received via mailing list)
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 Whittaker wrote:
>(2) In my controller perform a query that searches every model for every
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-04-25 17:03
Kevin Olbrich 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 Whittaker wrote:
>>(2) In my controller perform a query that searches every model for every
>>Posted via http://www.ruby-forum.com/.
>>_______________________________________________
>>Rails mailing list
>>Rails@lists.rubyonrails.org
>>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.
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-05-09 11:38
James Whittaker wrote:
> Kevin Olbrich 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 Whittaker wrote:
>>>(2) In my controller perform a query that searches every model for every
>>>Posted via http://www.ruby-forum.com/.
>>>_______________________________________________
>>>Rails mailing list
>>>Rails@lists.rubyonrails.org
>>>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?
This topic is locked and can not be replied to.