# Creation of Table Data From Rake

**URL:** <https://www.ruby-forum.com/t/creation-of-table-data-from-rake/172261>\
**Category:** Ruby\
**Created:** [July 17, 2009, 10:55pm UTC](https://www.ruby-forum.com/t/creation-of-table-data-from-rake/172261 "2009-07-17T22:55:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 17, 2009, 10:55pm UTC](https://www.ruby-forum.com/t/creation-of-table-data-from-rake/172261/1 "2009-07-17T22:55:42Z")

</div>

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](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…

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 17, 2009, 11:46pm UTC](https://www.ruby-forum.com/t/creation-of-table-data-from-rake/172261/2 "2009-07-17T23:46:59Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 18, 2009, 12:51am UTC](https://www.ruby-forum.com/t/creation-of-table-data-from-rake/172261/3 "2009-07-18T00:51:37Z")

</div>

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/](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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 18, 2009, 6:00am UTC](https://www.ruby-forum.com/t/creation-of-table-data-from-rake/172261/4 "2009-07-18T06:00:06Z")

</div>

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.
