You might look at the methods that Active Record provides for this.

* http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html

ie. : Person.sum('age') # => 4562

Cheers,
Robby

On Wed, Mar 4, 2009 at 3:42 PM, esdevs <seanpdev...@gmail.com> wrote:
>
> right - so I could do something like
> <% for user in @users%>
> <%= user.deals.collect(&:rev).sum%>
> <% end %>
>
> which gives the total sum of deals (but not yet iterated through
> months which is then the goal)...I'll keep working on it unless you
> see something very obvious -- by the way thanks for all of your
> insight
>
> On Mar 4, 5:23 pm, Colin Law <clan...@googlemail.com> wrote:
>> 2009/3/4 esdevs <seanpdev...@gmail.com>
>>
>>
>>
>> > >>objecting to user.deals.keys
>> > so yeah if I switch things up (and not iterate but rather define @user
>> > as User.find(:first).  It works great for the first user in the array
>> > perfectly well (code below).  Its just when I want to iterate over all
>> > of the users that it becomes an issue
>>
>> You need
>> <% for user in @users  %>
>> as you originally had to select each user, but then user.deals will give you
>> an array of Deals for that user which you can then process.  There is no
>> need to do @user.deals.find().  Unless I am missing something.  I would
>> suggest getting it going to the point that you can see the deals ok without
>> worrying about the grouping for the moment.  Then add the grouping which I
>> think is just processing of the user.deals array.
>>
>>
>>
>> > >>worked through some of the plethora of tutorials...
>> > yeah - the tutorials are amazing and are what got me this far - I've
>> > come to this discussion board as a last resort for this particular
>> > piece of the app I'm working on.
>>
>> > Controller
>> > def index
>> >   �...@users = User.find :all, :order => 'name ASC'
>> >     @user = User.find(:first)
>> >   �...@user_groups = @user.deals.find(:all).group_by {|t|
>> > t.saledate.at_beginning_of_month}
>> >  end
>>
>> > View
>> > <ul id="monthly-revs">
>> >        <strong><li><%=h Time.now.year %></li></strong>
>> >   <% @user_groups.keys.sort.each do |month| %>
>> >     <li><%=h month.strftime('%B') %></li>
>> >     <li><%=h number_to_currency(@user_groups[month].collect
>> > (&:rev).sum, :precision => 0) %></li>
>> >  <% end %>
>> > </ul>
>>
>> > On Mar 4, 4:44 pm, Colin Law <clan...@googlemail.com> wrote:
>> > > OK, you don't need @deals = ...
>>
>> > > I guess it is objecting to user.deals.keys as user.deals is an array of
>> > > Deals for that user.
>>
>> > > Can I ask whether you have worked through some of the plethora of
>> > tutorials
>> > > on basic Rails?
>>
>> > > 2009/3/4 esdevs <seanpdev...@gmail.com>
>>
>> > > > yes - the relationship is Deals belongs to User and User has many
>> > > > Deals.  So I even changed the code accordingly (from deal_groups to
>> > > > deals) per below...what's wierd is that now I am getting an error for
>> > > > undefined method 'key's (also below)
>>
>> > > > Controller
>> > > > def index
>> > > >   �...@users = User.find :all, :order => 'name ASC'
>> > > >     @deals = Deal.find(:all).group_by {|t|
>> > > > t.saledate.at_beginning_of_month}
>> > > >  end
>>
>> > > > View
>> > > > <% for user in @users %>
>> > > > <ul id="monthly-revs">
>> > > >        <strong><li><%=h Time.now.year %></li></strong>
>> > > >   <% user.deals.keys.sort.each do |month| %>
>> > > >     <li><%=h month.strftime('%B') %></li>
>> > > >     <li><%=h number_to_currency(user.deals[month].collect
>> > > > (&:rev).sum, :precision => 0) %></li>
>> > > >  <% end %>
>> > > > </ul>
>> > > > <% end %>
>>
>> > > > Error
>> > > > NoMethodError in Users#index
>>
>> > > > Showing app/views/users/index.html.erb where line #19 raised:
>>
>> > > > undefined method `keys' for #<Class:0x217ed80>
>> > > > Extracted source (around line #19):
>>
>> > > > 16: <% for user in @users %>
>> > > > 17: <ul id="monthly-revs">
>> > > > 18:     <strong><li><%=h Time.now.year %></li></strong>
>> > > > 19:   <% user.deals.keys.sort.each do |month| %>
>> > > > 20:     <li><%=h month.strftime('%B') %></li>
>> > > > 21:     <li><%=h number_to_currency(user.deals[month].collect
>> > > > (&:rev).sum, :precision => 0) %></li>
>> > > > 22:  <% end %>
>>
>> > > > On Mar 4, 3:38 pm, Colin Law <clan...@googlemail.com> wrote:
>> > > > > Have you setup relationships between the models, User has many
>> > DealGroups
>> > > > > and DealGroup belongs to User, or whatever is appropriate. Then you
>> > won't
>> > > > > need @deal_groups and can use user.deal_groups as you originally
>> > wrote.
>>
>> > > > > 2009/3/4 esdevs <seanpdev...@gmail.com>
>>
>> > > > > > ok so then I am where I am having difficulty is associating the
>> > user
>> > > > > > with the deal and displaying each user's deals.  So for example, if
>> > I
>> > > > > > change the code to the below, then it displays the total sum month
>> > to
>> > > > > > month as opposed to sum of each user month to month.
>>
>> > > > > > View
>> > > > > > <% for user in @users %>
>> > > > > > <ul id="monthly-revs">
>> > > > > >        <strong><li><%=h Time.now.year %></li></strong>
>> > > > > >   <% @deal_groups.keys.sort.each do |month| %>
>> > > > > >     <li><%=h month.strftime('%B') %></li>
>> > > > > >     <li><%=h number_to_currency(@deal_groups[month].collect
>> > > > > > (&:rev).sum, :precision => 0) %></li>
>> > > > > >  <% end %>
>> > > > > > </ul>
>> > > > > > <% end %>
>>
>> > > > > > Controller (is the same as above)
>> > > > > > def index
>> > > > > >   �...@users = User.find :all, :order => 'name ASC'
>> > > > > >   �...@deal_groups = Deal.find(:all).group_by {|t|
>> > > > > > t.saledate.at_beginning_of_month}
>> > > > > >  end
>>
>> > > > > > On Mar 4, 11:48 am, Colin Law <clan...@googlemail.com> wrote:
>> > > > > > > You appear to be setting up @deal_groups then not using it. You
>> > are
>> > > > using
>> > > > > > > user.deal_groups instead, which the error suggests is not
>> > defined.
>>
>> > > > > > > 2009/3/4 esdevs <seanpdev...@gmail.com>
>>
>> > > > > > > > so I am trying to sum month over month the amount that a user
>> > has
>> > > > > > > > posted.  So for example:
>> > > > > > > > User 1: Jan $3000 Feb $4000 March $1500, etc.  I can get this
>> > to
>> > > > work
>> > > > > > > > if I sum totals (aggregate of all users) but just not by user.
>>
>> > > > > > > > Here is my code in the controller:
>>
>> > > > > > > > def index
>> > > > > > > >   �...@users = User.find :all, :order => 'name ASC'
>> > > > > > > >   �...@deal_groups = Deal.find(:all).group_by {|t|
>> > > > > > > > t.saledate.at_beginning_of_month}
>> > > > > > > >  end
>>
>> > > > > > > > And then the code in the View
>> > > > > > > > <% for user in @users %>
>> > > > > > > > <ul id="monthly-revs">
>> > > > > > > >        <strong><li><%=h Time.now.year %></li></strong>
>> > > > > > > >  <% user.deal_groups.keys.sort.each do |month| %>
>> > > > > > > >    <li><%=h month.strftime('%B') %></li>
>> > > > > > > >    <li><%=h number_to_currency(user.deal_groups[month].collect
>> > > > > > > > (&:rev).sum, :precision => 0) %></li>
>> > > > > > > >  <% end %>
>> > > > > > > > </ul>
>> > > > > > > > <% end %>
>>
>> > > > > > > > Ultimately, I want to make this a partial but for now am
>> > getting
>> > > > the
>> > > > > > > > following error
>>
>> > > > > > > > NoMethodError in Users#index
>>
>> > > > > > > > Showing app/views/users/index.html.erb where line #19 raised:
>>
>> > > > > > > > undefined method `deal_groups' for #<User:0x2200114>
>> > > > > > > > Extracted source (around line #19):
>>
>> > > > > > > > 16: <% for user in @users %>
>> > > > > > > > 17: <ul id="monthly-revs">
>> > > > > > > > 18:     <strong><li><%=h Time.now.year %></li></strong>
>> > > > > > > > 19:   <% user.deal_groups.keys.sort.each do |month| %>
>> > > > > > > > 20:     <li><%=h month.strftime('%B') %></li>
>> > > > > > > > 21:     <li><%=h
>> > number_to_currency(user.deal_groups[month].collect
>> > > > > > > > (&:rev).sum, :precision => 0) %></li>
>> > > > > > > > 22:   <% end %>
>>
>> > > > > > > > Thanks in advance
>>
>> > > > > > > > esdevs
> >
>



-- 
Robby Russell
Chief Evangelist, Partner

PLANET ARGON, LLC
design // development // hosting w/Ruby on Rails

http://planetargon.com/
http://robbyonrails.com/
http://twitter.com/planetargon
aim: planetargon

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4068 [fax]

--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to