I’ve spent hours researching the subject and have tried many test
sequences in IRB, and rails console but I’m just having trouble making
headway into what is probably a very easy subject.
I understand arrays with at least 4 other languages but with Ruby I
haven’t found a mental connection with how I can assign variables to
arrays…
Take for example:
def calculate_tsos(model, datavar, teamvar, valvar)
var = model.compiled_this_week.find(:all)
var.each_with_index do |rows, i|
puts “#{model} [ Row #{i} | Team ID = #{rows.team_id} | Team =
#{rows.team.name} | #{datavar} = #{rows.send(datavar)}”
end
end
This will give me:
TotalOffense [ Row 0 | Team ID = 5 | Team = Tulsa | ydspgm = 569.86
TotalOffense [ Row 1 | Team ID = 47 | Team = Houston | ydspgm = 562.77
TotalOffense [ Row 2 | Team ID = 20 | Team = Oklahoma | ydspgm = 547.86
etc. etc.
So far, so good. I can at least see the data that I want.
Now, I want to assign the Team ID and the ydspgm to variables that I can
manipulate and use later on. Normally, I would think that I could do
something like this:
def calculate_tsos(model, datavar, teamvar, valvar)
var = model.compiled_this_week.find(:all)
var.each_with_index do |rows, i|
teamvar[i] = rows.team.id
valvar[i] = rows.send(datavar)
puts “#{model} [ Row #{i} | Team ID = #{teamid[i]} | Team =
#{rows.team.name} | #{datavar} = #{teamval[i]}”
end
end
But I get a…
undefined method `[]=’ for 0:Fixnum
So, before responding let me explain what I’ve been trying to do and
perhaps you can explain to me how I “should” be doing it rather than the
way I have been trying to do it:
Summary: Rake task is being used to pull data, calculate data, and save
data.
In my rake file I am performing the following:
update_tsos_offense = TsosOffense.new
update_tsos_offense.calculate_tsos(TotalOffense, “ydspgm”, to_team_id,
to_value)
This creates a new object in TsosOffense. The method it’s calling is
the one I posted above that I’m having some difficulties with. I’m
supplying the model name, the field, a variable to store the team_id in,
and a value to store the field information in.
I was going to call a ruby file in libs from TsosOffense to perform my
calculations but perhaps I could just do the calculations in this
method.
Once I do my calculations for this portion of the rake task, it needs to
be “held in queue” - not saved, just waiting to be saved…
There are 13 more iterations in the rake task calling 13 other models
and 13 other fields, further assigning variables.
When all 14 of these calls are finished, I want to save all 14 columns
of information to my new table which is the table in the TsosOffense
model…
However, I’m having great difficulty in trying to find a best practices
scenario for doing this. I learn more from code and by doing than I do
anything. Could someone please provide a short idea of how I could
accomplish this?
Thank you kindly.