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
Ruby on rails 类方法rails的按输出排序_Ruby On Rails_Activerecord_Scope - Fatal编程技术网

Ruby on rails 类方法rails的按输出排序

Ruby on rails 类方法rails的按输出排序,ruby-on-rails,activerecord,scope,Ruby On Rails,Activerecord,Scope,我试图在一个模型上输出一个由特定类方法排序的JSON响应。我知道我可以按实际属性排序,但如何通过发送块进行排序。也就是说: class Baker has_many :meals def this_week_cookies self.meals.this_week.sum(:cookies_baked) end end 我想打个电话 面包师,点{面包师{面包师{本周{饼干} 要在视图中输出排行榜,但我知道这不起作用,order方法正在编写SQL查询,因此它不能使用这样的自

我试图在一个模型上输出一个由特定类方法排序的JSON响应。我知道我可以按实际属性排序,但如何通过发送块进行排序。也就是说:

class Baker
  has_many :meals

  def this_week_cookies
    self.meals.this_week.sum(:cookies_baked)
  end
end
我想打个电话

面包师,点{面包师{面包师{本周{饼干}


要在视图中输出排行榜,但我知道这不起作用,order方法正在编写SQL查询,因此它不能使用这样的自定义方法。你需要使用ruby来做你想做的事情

Baker.all.sort\u by(&:本周饼干)