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
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 <arcima-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > 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 > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Walker
2009-May-13 21:17 UTC
Re: MVC, best practice for hash that combines objects?
Colin Law 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) -- Posted via http://www.ruby-forum.com/.
2009/5/13 Robert Walker <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> > Colin Law 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)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.> > > Line 2 generates the following SQL: > SELECT sum("entries".time) AS sum_time FROM "entries" WHERE > ("entries".task_id = 1) > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>> 2009/5/13 Robert Walker <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > >> >> Colin Law 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) > > > 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. > > >> >> >> Line 2 generates the following SQL: >> SELECT sum("entries".time) AS sum_time FROM "entries" WHERE >> ("entries".task_id = 1) >> -- >> Posted via http://www.ruby-forum.com/. >> >> >> > > > >-- Aim: arcxweb Skype: alexscott1 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---