Hi Colin,
I created rspec_scaffolds with both the teams and rushing_offenses
table. It was pluralized - my apologies. Here’s the schema:
Teams Table:
class CreateTeams < ActiveRecord::Migration
def self.up
create_table :teams do |t|
t.string :name, :unique => true
t.timestamps
end
end
def self.down
drop_table :teams
end
end
Rushing Offenses Table:
class CreateRushingOffenses < ActiveRecord::Migration
def self.up
create_table :rushing_offenses do |t|
t.integer :team_id
t.integer :games
t.integer :carries
t.integer :net
t.integer :tds
t.integer :wins
t.integer :losses
t.integer :ties
t.datetime :compiled_on
t.timestamps
end
end
def self.down
drop_table :rushing_offenses
end
end
The models are:
class Teams < ActiveRecord::Base
has_many :rushing_offenses
end
Didn’t realize that the scaffold would pluralize that - strange.
class RushingOffense < ActiveRecord::Base
belongs_to :team
end
===============================================
Now, I won’t post every bit of code because it would take away from the
topic at hand. But, I use a rake task that is run by cron to call a
scraper.rb file through the rushing_offense model to parse data from
other sites which it then populates my rushing_offense table with.
However, that was before I made changes so it has to be done differently
now.
So, it looks similar to this:
Rake -> update_rushing = RushingOffense.new
–> update_rushing.scrape(arguments)
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)
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.
I will more than likely populate the teams table myself to create the
default 120 teams and their IDs. But, all of my stat tables need to
know that when a record is created, the team_id needs to match the id in
the teams table. Again, I just don’t know how to create this query
based on my examples above.
Keep in mind that the rake task runs weekly so it populates new data
each and every week for each of the stat tables.
In the end, my tables should look like so:
Teams
id = 1
name = Alabama
id = 2
name = Auburn
etc.
up to 120 teams
Rushing_Offenses
team_id = 1
statone = 100
stattwo = 200
statthree = 300
team_id = 2
statone = 100
stattwo = 200
statthree = 300
etc…
up to 120 teams for that week’s rake task
The next week will do the same thing for Rushing Offenses, only the
differing data will be the time it was created/compiled on (which is the
search parameter I use on my views).
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?
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.
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. A lot of it
has to do with the wordiness of the articles and the way my mind works.
They talk about orders and customers and I have to change the
associations to what I’m working with in order to understand it a lot of
times.
I’m just better with reading code than I am with anything. If you give
me a paragraph describing how standard deviation works for instance, it
would go over my head. If you write out a standard deviation formula, I
understand it perfectly.