I’m having a little trouble with understanding how to work out the
schematic for some of my classes using ActiveRecord when a file is in my
lib directory:
Brief example:
Here’s the outline of the files in use:
…app
…controllers
…application_controller.rb
…rushing_offenses_controller.rb
…models
…rushing_offense.rb
…lib
…scraper.rb
…tasks
…scraper.rake
The rushing_offense.rb file contains:
class RushingOffense < ActiveRecord::Base
end
The scraper.rb file contains:
class Scraper < ActiveRecord::Base
METHOD that define which URL to parse
METHOD that parses the data into an instanced variable called @rows
METHOD that should be updating my database table called
“rushing_offenses”
Update Rushing Offense
def update_rushing_offense
for i in 0…@numrows-1
update_all(:name => @rows[i][0], :games => @rows[i][1])
puts “Updating Team Name = #{@rows[i][0]}.”
end
end
end
The scraper.rake file contains:
desc “This task will parse data from ncaa.org and upload the data to our
db”
task :scraper => :environment do
offensive_rushing =
Scraper.new(‘http://web1.ncaa.org/mfb/natlRank.jsp?year=2008&rpt=IA_teamrush&site=org’,
‘table’, ‘statstable’, ‘//tr’)
offensive_rushing.scrape_data
offensive_rushing.clean_celldata
offensive_rushing.print_values
offensive_rushing.update_rushing_offense # the call to the method
above
end
Now if I run the rake file what is going to happen is I’m going to get
an error stating:
Table ‘project_development.scrapers’ doesn’t exist:
I believe I understand why that’s happening but I’m not sure how to fix
it from a long term perspective. Here’s why…
The class Scraper is pushed into the ActiveRecord::Base so it believes
the class is the pluralized name of the table Scrapers. I then thought
well maybe I need to put the code in the rushing_offenses_controller.rb
file in that class but here’s the issue I’m having:
The Scraper class should be a class that I can call with other classes
to do repetitive tasks on many different URLs. I’ve setup the class to
do that with the methods being able to retrieve different URLs.
So, I want my Scraper class to just act like a utility class to be used
by other classes to parse data, and upload it to the correct database
table. If I place the scraper class inside the
rushing_offenses_controller file then I’m not following DRY principles.
I don’t want to have to repeat code over and over.
Any ideas on how I can rectify this issue I’m having?