Wow, what a week! I’ve gotten a lot accomplished with my site but
brain-fried has set into my head…
I’m reaching a point where I’m having to manipulate arrays to gain
access to ids that I will use in model.find methods.
Here’s a quick and probably dirty example of what works. I’m only
showing it here because I know it’s gotta be dirty and ugly and there’s
probably a better way of doing it, but I can’t seem to figure it out.
My best guess is I should be using each and each_with_index but anyhoo…
Providing the methods as is, with a brief idea of what data is
returned…
open_schedule = Schedule.new
opp_scheduled_ids = []
120.times do |i|
opp_scheduled_ids[i+1] = open_schedule.find_opponents(i+1)
puts “Team ID = #{i+1} and Scheduled IDs are
#{opp_scheduled_ids[i+1].join(’,’)} and Array Size =
#{opp_scheduled_ids[i+1].size}”
end
This looks into my schedule model/table, finds 120 teams and any
opponent they’ve scheduled within date > datetime value and date <
datetime value. It then returns the ids for those opponents. Data looks
like this:
Team ID = 1 and Scheduled IDs are
25,27,65,120,6,62,119,92,109,100,121,111,9 and Array Size = 13
Team ID = 2 and Scheduled IDs are
54,85,51,29,110,116,106,22,19,113,48,49 and Array Size = 12
Team ID = 3 and Scheduled IDs are
100,58,52,91,27,68,74,111,115,32,118,54 and Array Size = 12
Team ID = 4 and Scheduled IDs are
121,99,78,89,105,33,35,108,73,92,103,56 and Array Size = 12
etc…
So, I’m able to hold the Team ID for the team I’m checking, and all of
their opponent_ids that they’ve scheduled based off whatever date
parameters I’ve selected. opponent_id is a foreign key that really is
the same as team_id but in an opponent’s column.
So far so good. Again, might be sloppy…
Now I want to pull out each of the opponent_ids that are stored in each
team’s array…
120.times do |i|
opp_scheduled_ids[i+1].size.times do |x|
puts “Team ID = #{i+1} and opp_id = #{opp_scheduled_ids[i+1][x]}”
end
end
This gives me the correct information that I’m looking for:
Team ID = 1 and opp_id = 25
Team ID = 1 and opp_id = 27
Team ID = 1 and opp_id = 65
Team ID = 1 and opp_id = 120
Team ID = 1 and opp_id = 6
Team ID = 1 and opp_id = 62
Team ID = 1 and opp_id = 119
Team ID = 1 and opp_id = 92
Team ID = 1 and opp_id = 109
Team ID = 1 and opp_id = 100
Team ID = 1 and opp_id = 121
Team ID = 1 and opp_id = 111
Team ID = 1 and opp_id = 9
Team ID = 2 and opp_id = etc…
etc…
So, what I plan on doing is issuing a find method for each opponent id
to find out a rating in a specific table and sum them all up. The find
method will go in this laste method that I showed you that only houses a
puts statement so far…
But, as you can see that method is very ugly…
What can I do to clean it up so that it still does the same thing but
with cleaner code or efficiency? Again, my purpose is to learn better
array management with rails, especially since I will be doing a find
command.
In addition, my concerns are that I could probably just do “one” find
command for all of the rating values in a specific table and then search
within those results, matching up the opponent_id to the team_id and the
value…
Example:
I want to search for the totals for team_id in offense ratings table.
Do a find(:all) to pull all totals in offense ratings table.
Find the totals for opp_id = # where team_id = #…
This would be better, IMO, because I’m not pulling 120 separate queries
but one large query and searching in the cached results… correct?
Any advice will be greatly “appreciated” regarding my concerns…
Hey Marnen… I remembered