Creation of Table Data From Rake

I have been posting a topic on the Rails forum but it might not be the
right area to post. Therefore, I believe my issue might be more related
to Ruby and procedural matters with Rake.

To make things as clear as possible, I’ve pasted some sandbox code here:

http://pastie.org/549855

The issues that I’m trying to concentrate on are located within the rake
task around lines 17-20, and also in the StatModel class around lines
71-86.

The basic procedural breakdown of this code is as follows:

Rake Task is performed.
Rake Task opens a new object with StatModel
Rake Task calls method calculate_rating on StatModel
StatModel method responds by pulling data from TableModel class
StatModel open a new object with MathClass
MathClass looks over the dataset and returns the following:
min, max, mean, standard deviation, and floor values

  • these values continue to persist…
    StatModel calls a new method on MathClass to calculate ratings
    MathClass takes persistant data and dataset and calculates ratings
    MathClass returns foreignkey for TableModel along with Rating
    StatModel sends returned data back to Rake
    Rake Task now has all data in its hands to do what it pleases with.
    – STUCK –

How do I send all data returned back to StatModel.table_update and how
does table_update save this data…

The code will show you everything that applies to this situation. The
routine works pretty smoothly. I’m just stuck with how to iterate over
all of the data returned, categorizing it by the foreign_keys and saving
all data as a combined dataset to the StatModel table…

See the puts example for what rake sees when the values are returned…

I added a simple loop at the very bottom of the pastie code which shows
you all of the id and rating values for all 28 arrays (14 paired). I
did this to show that I do indeed have the data in rake. I just don’t
know what to do with it now that I have it and how to save it in the
open model object…

On Jul 17, 2009, at 5:48 PM, Älphä Blüë wrote:

I added a simple loop at the very bottom of the pastie code which
shows
you all of the id and rating values for all 28 arrays (14 paired). I
did this to show that I do indeed have the data in rake. I just don’t
know what to do with it now that I have it and how to save it in the
open model object…


Posted via http://www.ruby-forum.com/.

I just posted this to your thread on the rails list, but I’ll answer
here, too.

Put this at the end of your pastie and see if it helps you see.

referencing a new key causes an empty has to be stored as the value

stats = Hash.new {|h,k| h[k] = {} }

i will take on the same values as 0.upto(119)

120.times do |i|
stats[to_team_id[i]][:to] = to_ppcs[i]
stats[ro_team_id[i]][:ro] = ro_ppcs[i]
stats[po_team_id[i]][:po] = po_ppcs[i]
stats[so_team_id[i]][:so] = so_ppcs[i]
stats[rzo_team_id[i]][:rzo] = rzo_ppcs[i]
stats[flo_team_id[i]][:flo] = flo_ppcs[i]
stats[pio_team_id[i]][:pio] = pio_ppcs[i]
stats[too_team_id[i]][:too] = too_ppcs[i]
stats[sao_team_id[i]][:sao] = sao_ppcs[i]
stats[tflo_team_id[i]][:tflo] = tflo_ppcs[i]
stats[peo_team_id[i]][:peo] = peo_ppcs[i]
stats[fdo_team_id[i]][:fdo] = fdo_ppcs[i]
stats[tdco_team_id[i]][:tdco] = tdco_ppcs[i]
stats[fdco_team_id[i]][:fdco] = fdco_ppcs[i]
end

puts stats.inspect

or this might be easier to read

require ‘pp’
pp stats

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Rob,

Thanks mate. I wanted to follow-up over here as well since you really
did a great job helping me out with this issue. Here’s what I ended up
doing:

Okay, after testing and testing, I finally managed to get it all to
work. However, I’m sure my way is very clumsily implemented but it was
the only way I understood how to read the values and place them into the
table.

I called the following from Rake:

update_tsos_offense.table_update(TsosOffense, stats) # model, # array

And in the model for table_update I did:

def table_update(model, array)

if model.compiled_this_week.find(:all).empty?
puts “Updating #{model} for the following teams:”
120.times do |i|
team = Team.find(i + 1)
values = {:compiled_on => Date.today.strftime(’%Y-%m-%d’)}
values[:team_id] = i + 1
values[:totoff] = array[i + 1][:totoff]
values[:rushoff] = array[i + 1][:rushoff]
values[:passoff] = array[i + 1][:passoff]
values[:scoroff] = array[i + 1][:scoroff]
values[:rzonoff] = array[i + 1][:rzonoff]
values[:fumlost] = array[i + 1][:fumlost]
values[:passhint] = array[i + 1][:passhint]
values[:tolost] = array[i + 1][:tolost]
values[:sacksall] = array[i + 1][:sacksall]
values[:tackflossall] = array[i + 1][:tackflossall]
values[:passeff] = array[i + 1][:passeff]
values[:firdwns] = array[i + 1][:firdwns]
values[:thrdwncon] = array[i + 1][:thrdwncon]
values[:fthdwncon] = array[i + 1][:fthdwncon]
model.create values
puts “#{team.name} values are being saved.”
end
else
# data is already populated for the week so don’t update
puts “Current Week’s Ratings are Already updated!”
end
end

I had to add 1 because the i count started at 0. I also couldn’t use
the constant or iterate using each or each_with_index because one, I
couldn’t get it to sort.

This way does work though and so I’m happy that it at least is
functioning. Although, I’m sure it can use some cleanup. Please let me
know if anyone sees a way for me to make this code a bit cleaner.

Many thanks Rob.