Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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
Mysql 铁路及;JBuilder-作为字符串输出的十进制(10,2)数据类型_Mysql_Ruby On Rails_Json - Fatal编程技术网

Mysql 铁路及;JBuilder-作为字符串输出的十进制(10,2)数据类型

Mysql 铁路及;JBuilder-作为字符串输出的十进制(10,2)数据类型,mysql,ruby-on-rails,json,Mysql,Ruby On Rails,Json,我继承了一个Rails应用程序,但遇到了麻烦,我没有Rails方面的经验。它使用JBuilder从MySQL数据库返回JSON响应。我更新了应用程序,在数据库中添加了两个新列,类型为DECIMAL(10,2)。问题是,当我请求JSON时,值作为字符串返回。如果我使用DECIMAL(10,0),那么它将作为一个数字返回。有没有办法强制JBuilder返回一个数字?以下示例中的相关参数为game_score 控制器的相关部分: # GET /game # GET /game.json d

我继承了一个Rails应用程序,但遇到了麻烦,我没有Rails方面的经验。它使用JBuilder从MySQL数据库返回JSON响应。我更新了应用程序,在数据库中添加了两个新列,类型为
DECIMAL(10,2)
。问题是,当我请求JSON时,值作为字符串返回。如果我使用
DECIMAL(10,0)
,那么它将作为一个数字返回。有没有办法强制JBuilder返回一个数字?以下示例中的相关参数为game_score

控制器的相关部分:

  # GET /game
  # GET /game.json
  def index
    if params[:since]
      since_datetime = Time.parse(params[:since])
      @game = current_user.game.where(["updated_at >= ? and deleted is not true",  since_datetime])
    else
      @game = current_user.game.where('deleted is not true')
    end

  end
index.json.jbuilder文件:

json.array!(@game) do |game|
   json.extract! game, :id, :game_date, :game_score, :game_location, :game_team, :game_referee
   json.url game_url(game, format: :json)
end
MySQL数据类型为十进制(10,2)时的JSON:

MySQL数据类型为十进制(10,0)时的JSON:


最简单的方法是手动写入此字段,如:

json.game_score game.game_score.to_f
[{"id":4814,"game_date":"2014-09-10T15:51:00.000Z","game_score":1,"game_location":null,"game_team":"Team 1","game_referee":null, "url":"http://[ip]/game/4814.json"}]
json.game_score game.game_score.to_f