草庐IT

Update-Upsert

全部标签

mysql - rails update_all 字段并将计数递增 1

我的mysql表中有一列名为download_count,类型为int。现在我想使用update_all用一些值更新一些字段,同时增加download_count。我试过以下语法:find_record.update_all(username:username,download_date:download_date,"download_count=download_count+1")但我相信上面的行为就像一个where子句。所以download_count不会更新。我希望通过上述查询更新用户名、下载日期和下载计数。谁能告诉我正确的语法? 最佳答案

ruby-on-rails - rails : How do I cancel a save if before_update callback returns false?

我有一个模型,它有一个before_update回调。根据我的理解,当在模型实例上调用update_attributes时,将调用before_update。我的假设是,如果before_update回调返回false,则不会更新记录。然而,这似乎并没有像假设的那样工作。每次我调用update_attributes时,即使before_update返回false,记录也会被保存。如果before_update返回false,您知道如何防止更新记录吗?这是我在user.rb文件中尝试过的内容:classUsertruebefore_updatedofalseendend这是我在Rails

ruby-on-rails - update_all 不工作

我正在使用update_all直接更新数据库中的特定记录。我在解析csv文件的脚本中执行此操作。我知道update_all返回一个关于更改了多少行的整数。有些行返回1,有些行返回0,这意味着没有行受到影响。attrs=listofuserattributesUser.update_all(attrs,{id:user.id})提前致谢! 最佳答案 这通常发生在数据库找不到特定行时,在本例中,该行基于user.id。我敢打赌您在用户上有一个default_scope正在过滤掉该特定用户。尝试使用User.unscoped.update

ruby-on-rails - rvm update 和 rvm remove 中的权限被拒绝

在ubuntu12.04上安装了rvm1.19.6和ruby2.0.0。在执行rvmgetstable时收到permissiondenied。这是错误:$rvmgetstable########################################################################100.0%bash:line535:/home/ubuntu/.rvm/RELEASE:PermissiondeniedCouldnotupdateRVM,getsomehelpat#rvmIRCchannelatfreenodeservers.当我们尝试在服务器上安

ruby-on-rails - update_all 方法

假设我有一个模型:classResult而不是做Result.all.find_eachdo|s|s.sum=compute_sum(s.x,s.y)s.saveend假设compute_sum是一种可用的方法,并进行一些无法转换为SQL的计算。defcompute_sum(x,y)sum_table[x][y]end有没有办法使用update_all,大概是这样的:Result.all.update_all(sum:compute_sum(:x,:y))我有超过80,000条记录要更新。find_each中的每条记录都创建自己的BEGIN和COMMIT查询,并且每条记录都单独更新。或

ruby-on-rails - 具有 mongoid 的许多对象的 update_attributes

可能是这样的:@users=User.criteria.for_ids(params[:user_ids])@users.update_all(:suspend=>true)代替:foruin@usersu.suspend=trueu.update_attributesend 最佳答案 看看这个:#Updatingonerecord:Person.update(15,:user_name=>'Samuel',:group=>'expert')#Updatingmultiplerecords:people={1=>{"first_na

ruby-on-rails - 私有(private)方法 `update' 调用 #<Customer

每当我尝试对我的Customer类进行更新时,我总是收到调用私有(private)方法“更新”的消息。应用跟踪:app/controllers/customers_controller.rb:46:在“更新”中所以,在代码中它在这个函数中:43defupdate44@customer=Customer.find(params[:id])4546if@customer.update(customer_params)47redirect_to@customer48else49render'edit'50end51end因此,我假设此问题发生在我的客户模型中,即:classCustomer然

ruby-on-rails - Rails Relation#update_all 比 Relation#update 消耗更多的内存

Relation#update(id,attributes)文档提到“无论对象是否成功保存到数据库,都会返回结果对象。”,而Relation#update_all(updates,conditions=nil,options={})表示没有进行任何实例化、回调或验证(直接数据库查询)。运行一个简单的性能测试并将Relation#update替换为Relation#update_all,看起来内存消耗增加了。有什么解释吗?摘录:#BeforeMyModel.update(my_hash['id'],special_attrs)#After(morememoryconsumed)MyMod

ruby-on-rails - 从 Rails 4.2 使用 UPSERT PostgreSQL

如何在PostgreSQL9.5onRails4.2中使用“UPSERT”或“INSERTINTOlikes(user_id,person_id)VALUES(32,64)ONCONFLICT(user_id,person_id)DONOTHING”? 最佳答案 在这里查看active_record_upsertgem:https://github.com/jesjos/active_record_upsert.这是upsert,但显然只适用于Postgres9.5+。 关于ruby-o

ruby - 应用引擎 : Launching a script upon update/run

我正在使用AppEngine,我正在考虑在我的下一个项目中使用LESSCSS扩展。没有用Python编写的好的LESSCSS库,所以我继续使用原始的Ruby库,它工作得很好而且开箱即用。我希望AppEngine在运行开发服务器之前和将文件上传到云端之前执行lessc./templates/css/style.less。自动化这个的最好方法是什么?我在想:#run.sh:lessc./templates/css/style.less.gae/dev_appserver.py--use_sqlite.和#deploy.shlessc./templates/css/style.less.ga