[...]
RushingOffense model houses a scrape method that calls a file called
scraper.rb and parses out the data from the site into an array of
arrays. It then creates the new data, populating the data using
something similar:
RushingOffense\.create\(:rank => offensive\_rushing\.rows\[i\]\[0\],
:name => offensive\_rushing\.rows\[i\]\[1\],
:games => offensive\_rushing\.rows\[i\]\[2\],
:carries => offensive\_rushing\.rows\[i\]\[3\],
:net => offensive\_rushing\.rows\[i\]\[4\],
:avg => offensive\_rushing\.rows\[i\]\[5\],
:tds => offensive\_rushing\.rows\[i\]\[6\],
:ydspg => offensive\_rushing\.rows\[i\]\[7\],
:wins => offensive\_rushing\.rows\[i\]\[8\],
:losses => offensive\_rushing\.rows\[i\]\[9\],
:ties => offensive\_rushing\.rows\[i\]\[10\],
:compiled\_on => Time\.now\)
---begin style-Nazi digression---
Just as an aside: this is rather unidiomatic Ruby. Why didn't you
assign offensive_rushing.rows[i] to a local variable and save some
typing (and possibly some calculation time)?
Worse, the [i] makes me think that you're using a for loop of some
sort. Remember, Ruby is not PHP, and it has different looping
constructs. You want rows.each.
Or you could do something like this:
FIELDS = [:rank, :name, :games, ...]
offensive_rushing.rows.each do |row|
values = {:compiled_on => Time.now}
FIELDS.each_with_index do |field, i|
values[field] = row[i]
end
RushingOffense.create values
end
...which is shorter and arguably easier to read.
-----end style-Nazi digression-----
Also note that you're doing a separate DB query for each record
created, which is a terrible idea (queries don't belong inside
loops). Investigate ar-extensions or some other bulk insertion
plugin, or if all else fails, generate a bulk insert statement
directly in SQL.
Now, this has all changed because by normalizing my tables, I moved the
name into the Teams table. I also removed rank (because it wasn't fully
dependent on the primary key). I also removed avg and ydspg because I
could gain these values by doing calculations later on and so they also
were not dependent on the primary key). So, by normalizing my tables it
has created issues for me in terms of how I scrape and populate the
tables.
I'm just not sure how to tie the two tables in when creating a record.
Simple:
team_name = offensive_rushing.rows[i][1] # (or whatever the proper
array subscript is)
team = Team.find_by_name(team_name)
# and then either:
team.rushing_offenses << RushingOffense.create(:whatever =>
'data', :you => 'want')
# or:
RushingOffense.create(:team => team, :other => 'data')
Rails does most of the work for you.
[...]
So, how do I create one single row of data using the example above if
the Teams table is pre-populated with 120 unique IDs and Teams and the
Rushing Offenses table needs to know which team_id it belongs to?
That's how.
When parsing the data, all information is stored in the arrays so the
name of the team is shown. I would imagine that the name of the team
would have to be matched to the team name in the teams column and that
the id would then be passed back to the team_id, or I could be
completely mistaken.
You are completely correct. That's what the Team.find_by_name line
above does.
As I have a very unique parsing mechanism, and I've read a lot of
articles now, some of this just doesn't make sense to me.
Your parsing mechanism is irrelevant. You just need to know what to
do with the data once parsed.
Best,