Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails中的自定义属性_Ruby On Rails_Ruby_Postgresql - Fatal编程技术网

Ruby on rails rails中的自定义属性

Ruby on rails rails中的自定义属性,ruby-on-rails,ruby,postgresql,Ruby On Rails,Ruby,Postgresql,我有一个学生模型和一个进行计算并返回值的方法 class Student < ActiveRecord::Base def total_result #some calculations return result end end 但这带来了一个PG::UndefinedColumn:ERROR。我正在使用postgres。如何实现这一点?您可以使用: Student.select { |student| student.total_result >

我有一个学生模型和一个进行计算并返回值的方法

class Student < ActiveRecord::Base
  def total_result
     #some calculations
     return result
   end
end
但这带来了一个PG::UndefinedColumn:ERROR。我正在使用postgres。如何实现这一点?

您可以使用:

Student.select { |student| student.total_result > params[:result] }
警告:这将从数据库中加载所有学生,并计算每个学生的值。这将是缓慢的,取决于表中的学生人数


如果您更频繁地需要它,那么在数据库中存储/缓存计算结果是有意义的。

只能查询表的属性,不能查询方法。你能解释一下你想做什么吗?我正在尝试接受用户输入,进行一些计算,然后将匹配的行返回给计算值。当你说返回匹配的行时,你试图将用户输入与哪个属性匹配。有人提供了我想要的东西。请在下面查看。谢谢这正是我想要的。性能目前不太受关注。谢谢
Student.select { |student| student.total_result > params[:result] }