Creation of Table Data From Rake

  • Thread starter Älphä Blüë
  • Start date
Ã

Älphä Blüë

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..
 
Ã

Älphä Blüë

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..
 
R

Rob Biedenharn

I added a simple loop at the very bottom of the pastie code which =20
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..

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

I just posted this to your thread on the rails list, but I'll answer =20
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 =3D Hash.new {|h,k| h[k] =3D {} }
# i will take on the same values as 0.upto(119)
120.times do |i|
stats[to_team_id][:to] =3D to_ppcs
stats[ro_team_id][:ro] =3D ro_ppcs
stats[po_team_id][:po] =3D po_ppcs
stats[so_team_id][:so] =3D so_ppcs
stats[rzo_team_id][:rzo] =3D rzo_ppcs
stats[flo_team_id][:flo] =3D flo_ppcs
stats[pio_team_id][:pio] =3D pio_ppcs
stats[too_team_id][:too] =3D too_ppcs
stats[sao_team_id][:sao] =3D sao_ppcs
stats[tflo_team_id][:tflo] =3D tflo_ppcs
stats[peo_team_id][:peo] =3D peo_ppcs
stats[fdo_team_id][:fdo] =3D fdo_ppcs
stats[tdco_team_id][:tdco] =3D tdco_ppcs
stats[fdco_team_id][:fdco] =3D fdco_ppcs
end

puts stats.inspect
# or this might be easier to read
require 'pp'
pp stats


-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
Ã

Älphä Blüë

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top