Skip to content

Instantly share code, notes, and snippets.

@eduardopoleo
Created July 24, 2016 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardopoleo/1df8be430c8acf1beaf335094c58b718 to your computer and use it in GitHub Desktop.
Save eduardopoleo/1df8be430c8acf1beaf335094c58b718 to your computer and use it in GitHub Desktop.
#lib/tasks/scrape.rake
def salary_averages(staff, use_case)
universities = staff.map{|p| p.university}.uniq
data = []
universities.each do |university|
university_staff = staff.select{|s| s.university == university}
average_salary = (university_staff.map{|p| p.salary}.reduce(:+)/university_staff.count).round(2)
Average.create(
university: university,
use_case: use_case,
average_salary: average_salary
)
end
end
#Each of the following queries will filter the records by case. A use_case parameter is passed down so that we can refer to it later in the controller endpoints.
puts "------------>Calculating overall salaries<---------------"
staff = Staff.all
salary_averages(staff, "overall_salaries")
puts "------------>Calculating Professors only<---------------"
staff = Staff.where("title like ?", "%Professor%")
salary_averages(staff, "professors_only")
puts "------------>Calculating Administrative only<---------------"
staff = Staff.where("title not like ?", "%Professor%")
salary_averages(staff, "administrative_only")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment