Here''s my current loop: update_total = model.find(:all) update_total.each do |row| val = ((row.totoff + row.rushoff + row.passoff + row.scoroff + row.rzonoff + row.fumlost + row.passhint + row.tolost + row.sacksall + row.tackflossall + row.passeff + row.firdwns + row.thrdwncon + row.fthdwncon)/14.0).round_to(4) row.totals = val row.save puts "Team = #{row.team.name} and total val = #{val}." end This of course looks sloppy. I thought I could use something similar: val = row.inject(0){|sum,item| sum + item} .. but I can''t figure out how to use this without it adding specific columns, namely.. team_id, totals, compiled_on, created_at, updated_at .. these 5 columns should be exempt from sums.. It''s not overly important and I can work around it. I guess I just want to keep my code as clean as possible and didn''t know if there is a more preferred way to sum up a row''s totals in rails.. Thanks. -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Jul-18 18:41 UTC
Re: How to sum returned rows from a model using inject?
Älphä Blüë wrote: [...]> .. but I can''t figure out how to use this without it adding specific > columns, namely.. > > team_id, totals, compiled_on, created_at, updated_at > > .. these 5 columns should be exempt from sums.How about using [:column1, :column2, ...] - [:team_id, :totals, ...] ? Remember, Array#- actually works in Ruby. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Thanks Marnen, I had a feeling it was going to be simpler than I was trying to make it out to be. Appreciate the info.. I think I''ve learned more about arrays and hashes in ruby this week over anything, which is a good thing. -- Posted via http://www.ruby-forum.com/.
Craig Demyanovich
2009-Jul-19 02:49 UTC
Re: How to sum returned rows from a model using inject?
On Sat, Jul 18, 2009 at 12:50 PM, Älphä Blüë < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Here''s my current loop: > > update_total = model.find(:all) > update_total.each do |row| > > val = ((row.totoff + row.rushoff + row.passoff + row.scoroff + > row.rzonoff + row.fumlost + row.passhint + row.tolost + row.sacksall + > row.tackflossall + row.passeff + row.firdwns + row.thrdwncon + > row.fthdwncon)/14.0).round_to(4)Instead of querying the model for all of this data, I recommend encapsulating this calculation in the model itself. Also, I''d give meaningful names to 14.0 and 4 to clearly communicate what purpose they serve in the calculation. Then your loop could be: Model.all.each do |model| model.recalculate_total model.save! puts ... end -- Craig Demyanovich Mutually Human Software http://mutuallyhuman.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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the advice Craig. I''m always looking for better way to do things. Much appreciated. -- Posted via http://www.ruby-forum.com/.
Hi Craig, Did you mean something like this? Both methods are in the model.. def recalculate_totals(row) fields = 5.0 row.totals = ((row.kickret + row.puntret + row.netpunt + row.kickretdef + row.puntretdef) / fields).round_to(4) end model.compiled_this_week.all.each do |row| row.recalculate_totals(row) row.save! puts "Team = #{row.team.name} and total val = #{row.totals}." end -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Jul-19 13:47 UTC
Re: How to sum returned rows from a model using inject?
Älphä Blüë wrote:> Hi Craig, > > Did you mean something like this? > > Both methods are in the model.. > > def recalculate_totals(row)[...] You don''t need an argument on this method, since the row is already accessible as self. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Craig Demyanovich
2009-Jul-19 14:34 UTC
Re: How to sum returned rows from a model using inject?
Hi, Yes, and Marnen''s observation in a later message about not needing to pass row is spot on. Regards, Craig -- Craig Demyanovich Mutually Human Software http://mutuallyhuman.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 -~----------~----~----~----~------~----~------~--~---
Thanks guys. I changed it to: def recalculate_totals fields = 5.0 self.totals = ((self.kickret + self.puntret + self.netpunt + self.kickretdef + self.puntretdef) / fields).round_to(4) end and.. model.compiled_this_week.all.each do |row| row.recalculate_totals row.save! puts "Team = #{row.team.name} and total val = #{row.totals}." end -- Posted via http://www.ruby-forum.com/.