Created
July 24, 2016 17:31
-
-
Save eduardopoleo/1df8be430c8acf1beaf335094c58b718 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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