# MVC, best practice for hash that combines objects?

**URL:** <https://www.ruby-forum.com/t/mvc-best-practice-for-hash-that-combines-objects/167902>\
**Category:** Rails\
**Created:** [May 13, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/mvc-best-practice-for-hash-that-combines-objects/167902 "2009-05-13T20:07:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arcX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@arcX](https://www.ruby-forum.com/u/arcX)\
**Post date:** [May 13, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/mvc-best-practice-for-hash-that-combines-objects/167902/1 "2009-05-13T20:07:21Z")

</div>

Hi,

I just wanted to see if I am on the right track, as I am new to MVC.

I have a model called Tasks, and another called Entries. Tasks has  
many entries.

Each entry has a time. So I want to total up the time entries so that  
I have a keyed array of task Ids =\> duration.

In the end, I want to then print it out as a table in the reports  
view, doing things like filter by completed, by date, etc.

Where should this hash be constructed? A function in a Report model?

Thanks!  
arcX

---

<div class="post-metadata">

**Author:** ![arcX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@arcX](https://www.ruby-forum.com/u/arcX)\
**Post date:** [May 13, 2009, 10:33pm UTC](https://www.ruby-forum.com/t/mvc-best-practice-for-hash-that-combines-objects/167902/2 "2009-05-13T22:33:09Z")

</div>

Assuming you are developing in Rails then the convention would be for  
the  
models to be Task and Entry, with the controllers tasks\_controller and  
entries\_controller.

If I have understood the problem correctly I think the first thing I  
would  
do is provide a method in model Task called duration that returns the  
total  
duration for that task by summing task.entries.time. Then all you need  
to  
pass to the report view is @tasks containing the tasks you are  
interested in  
and for each one task.duration is available to be displayed as desired.

Colin

2009/5/13 arcX [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![arcX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@arcX](https://www.ruby-forum.com/u/arcX)\
**Post date:** [May 13, 2009, 11:17pm UTC](https://www.ruby-forum.com/t/mvc-best-practice-for-hash-that-combines-objects/167902/3 "2009-05-13T23:17:43Z")

</div>

Colin L. wrote:

> If I have understood the problem correctly I think the first thing I  
> would  
> do is provide a method in model Task called duration that returns the  
> total  
> duration for that task by summing task.entries.time. Then all you need  
> to  
> pass to the report view is @tasks containing the tasks you are  
> interested in  
> and for each one task.duration is available to be displayed as desired.

Rails also provides aggregate functions so rather than writing a  
duration method you could also write:  
task = Task.find(:first)  
task.entries.sum(:time)

Line 2 generates the following SQL:  
SELECT sum(“entries”.time) AS sum\_time FROM “entries” WHERE  
(“entries”.task\_id = 1)

---

<div class="post-metadata">

**Author:** ![arcX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@arcX](https://www.ruby-forum.com/u/arcX)\
**Post date:** [May 14, 2009, 1:28pm UTC](https://www.ruby-forum.com/t/mvc-best-practice-for-hash-that-combines-objects/167902/4 "2009-05-14T13:28:18Z")

</div>

Wow!Thanks so much to both of you, that works great for me!  
It also helped me to understand just how powerful the model can be.  
arcX

2009/5/14 Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > pass to the report view is @tasks containing the tasks you are  
> > > returns entries.sum(:time), as it removes the requirement that the caller
> 
> >

–  
Aim:  
arcxweb

Skype:  
alexscott1

---

<div class="post-metadata">

**Author:** ![arcX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@arcX](https://www.ruby-forum.com/u/arcX)\
**Post date:** [May 14, 2009, 9:07am UTC](https://www.ruby-forum.com/t/mvc-best-practice-for-hash-that-combines-objects/167902/5 "2009-05-14T09:07:04Z")

</div>

2009/5/13 Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > and for each one task.duration is available to be displayed as desired.
> 
> Rails also provides aggregate functions so rather than writing a  
> duration method you could also write:  
> task = Task.find(:first)  
> task.entries.sum(:time)

I would still suggest having a duration method, where the method just  
returns entries.sum(:time), as it removes the requirement that the  
caller  
needs to know that how to work out the duration.
