Reputation: 237
I have a Student model and a method that does some calculations and returns a value
class Student < ActiveRecord::Base
def total_result
#some calculations
return result
end
end
Now in my students controller I would like to do the following
Student.where("total_result > ?", params[:result])
but this brings a PG::UndefinedColumn: ERROR. I am using postgres. How do I achieve this?
Upvotes: 1
Views: 562
Reputation: 106832
You could use:
Student.select { |student| student.total_result > params[:result] }
A word of warning: This will load all students from the database and calculate the value for each of them. This will be slow depending on the number of students in the table.
If you need this more frequently then it would make sense to store/cache the result of the calculation in the database.
Upvotes: 1