简体   繁体   中英

syntax error, unexpected tCONSTANT, expecting ')'

I wrote a method and I received an error. This is my code:

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

Then I received this error message:

{
  "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 ')'"
      },

It says that occured a Syntax error at line 146. Line 146 as follows:

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

How can I solve this problem?

Just add quotes around your SQL snippet so that is is handled as a String by Ruby.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM