I have a DataPoint model where I have three methods that do the same thing, just on different fields in the data_points database.
def self.currently_employed(data_points)
data_points.where(currently_employed: true).count / data_points.count.to_f * 100
end
def self.undergrad_degree(data_points)
data_points.where(undergrad_degree: true).count / data_points.count.to_f * 100
end
def self.grad_degree(data_points)
data_points.where(grad_degree: true).count / data_points.count.to_f * 100
end
I think I should be able to refactor this to accept which column to perform the calculation on as a parameter, but much time on the internet / trying stuff out hasn't yielded results.
Could someone please advise me on whether / how I could refactor this to a single method?
In case it affects the answer: the data_points argument is a subset of DataPoint.all specified by the User - i.e. User types in what age, years work experience, etc. they want to see results for (age & years_work_experience are DataPoint attributes), and then I want to show summary stats for that subset.
Thanks!