Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.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 调用数据库函数以更新用户详细信息_Ruby On Rails_Ruby_Postgresql - Fatal编程技术网

Ruby on rails 调用数据库函数以更新用户详细信息

Ruby on rails 调用数据库函数以更新用户详细信息,ruby-on-rails,ruby,postgresql,Ruby On Rails,Ruby,Postgresql,在使用下面的代码更新用户密码时,是否有方法使用此PostgreSQL函数crypt(“”,gen_salt('bf')) def update @player = Player.find(params[:id]) respond_to do |format| if @player.update_attributes(params[:player]) flash[:notice] = 'Player was successfully updated.' f

在使用下面的代码更新用户密码时,是否有方法使用此PostgreSQL函数
crypt(“”,gen_salt('bf'))

def update
  @player = Player.find(params[:id])

  respond_to do |format|
    if @player.update_attributes(params[:player])
      flash[:notice] = 'Player was successfully updated.'
      format.html { redirect_to(@player) }
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @player.errors, :status => :unprocessable_entity }
    end
  end
end

至少有一种方法可以在update-update\u all方法中使用纯SQL

因此,要使用Postgres crypt方法更新玩家的密码,您可以执行以下操作:

Player.update_all "password = crypt('<password>', gen_salt('bf'))", "id = #{params[:id]}"
Player.update_all“password=crypt(“”,gen_salt('bf')),“id={params[:id]}”

为什么要将与应用程序相关的问题推到数据存储中?即使每次调用的性能更高,但当您尝试扩展时,您的数据库通常是您的瓶颈。@coreyward:true,但问题仍然很有趣。:-)