语法错误,意外的tconstant,应为“)”

cedebl8k  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(409)

我编写了一个方法,但收到了一个错误。这是我的密码:

def workouts_number
  @workout_block = BlockWorkout.all
  @number = Hash.new(0)

  @workout_block.each do |record|
    @current_workout_block = BlockWorkout.group(:workout_id).order(COUNT(workout_id) DESC).limit(5)
    @name = @current_workout_block['block_name']
    @number[@name] += 1
  end

  render :json => {data: {unprocessed: @workout_block, processed: @number}}
end

然后我收到了这个错误消息:

{
  "status": 500,
  "error": "Internal Server Error",
  "exception": "#<SyntaxError: /home/gabriel_rc/Área de Trabalho/Atlas/atlas-academia-back/app/controllers/workouts_controller.rb:146: syntax error, unexpected tCONSTANT, expecting ')'\n...d).order(COUNT(workout_id) DESC).limit(5)\n...                           ^~~~\n/home/gabriel_rc/Área de Trabalho/Atlas/atlas-academia-back/app/controllers/workouts_controller.rb:174: syntax error, unexpected end, expecting end-of-input>",
  "traces": {
    "Application Trace": [
      {
        "exception_object_id": 27087800,
        "id": 0,
        "trace": "app/controllers/workouts_controller.rb:146: syntax error, unexpected tCONSTANT, expecting ')'"
      },

它说在第146行发生了语法错误。第146行如下:

@current_workout_block = BlockWorkout.group(:workout_id).order(COUNT(workout_id) DESC).limit(5)

我怎样才能解决这个问题?

pgpifvop

pgpifvop1#

只需在sql片段周围添加引号,这样ruby就可以将其作为字符串处理。

@current_workout_block = BlockWorkout.group(:workout_id).order("COUNT(workout_id) DESC").limit(5)

相关问题