Rails:update_column 有效,但 update_attributes 无效 [英] Rails: update_column works, but not update_attributes

查看:59
本文介绍了Rails:update_column 有效,但 update_attributes 无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的模型:

class Reply < ActiveRecord::Base
  attr_accessible :body
  belongs_to :post
end

在我的控制器中,我有一个简单的更新方法:

In my controller, I have a simple update method:

def update
  @reply = Reply.find(params[:id])
  if @reply.update_attributes!(params[:reply])
    render :js => "alert('I am trying to update!')"
  else
    render :js => "alert('<%= @reply.errors %>')"
  end
end

这不会引发错误,但实际上也不会更新回复.相反,我得到了我正在尝试更新!"消息,就像一切正常.但是当我重新加载页面并查看回复时,它具有相同的文本.它实际上并没有更新.如果我将 update_attributes 替换为:

This doesn't throw an error, but neither does it actually update the reply. Instead, I get the "I am trying to update!" message, like everything worked. But when I reload the page and look at the reply, it has the same text. It hasn't actually been updated. If I replace update_attributes with:

@reply.update_column(:body, params[:reply][:body])

它工作正常.如果我使用:

It works fine. If I use:

@reply.update_attribute(:body, params[:reply][:body])

它再次不起作用.知道发生了什么吗?

It once again doesn't work. Any idea what's going?

在我的日志中,我有这个:

In my log, I have this:

Started PUT "/posts/2/replies/20" for 127.0.0.1 at 2013-01-19 10:39:57 -0600
Processing by RepliesController#update as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xot7E+ldXiBm0hVvw5XUP/U5guJU2g8e4QaLbDVGzDE=", "reply"=>{"body"=>"Updated text."}, "commit"=>"Submit Revision", "post_id"=>"2", "id"=>"20"
[1m[35mUser Load (1.0ms)[0m  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
[1m[36mReply Load (0.0ms)[0m  [1mSELECT `replies`.* FROM `replies` WHERE `replies`.`id` = 20 LIMIT 1[0m
[1m[35m (1.0ms)[0m  BEGIN
[1m[36mPost Load (0.0ms)[0m  [1mSELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 2 LIMIT 1[0m
[1m[35m (0.0ms)[0m  COMMIT
Rendered replies/_reply_content.html.erb (502.0ms)
Rendered replies/update.js.erb (505.0ms)
Completed 200 OK in 849ms (Views: 484.0ms | ActiveRecord: 94.0ms)

推荐答案

你使用的三种方法做不同的事情:

The three methods you are using do different things:

  • update_attributes 尝试验证记录,调用回调保存
  • update_attribute 不验证记录,调用回调保存
  • update_column 不验证记录,不调用回调,不调用 save 方法,尽管它确实更新记录在数据库中.
  • update_attributes tries to validate the record, calls callbacks and saves;
  • update_attribute doesn't validate the record, calls callbacks and saves;
  • update_column doesn't validate the record, doesn't call callbacks, doesn't call save method, though it does update record in the database.

如果有效"的唯一方法是 update_column,我的猜测是您在某处有一个回调会引发错误.尝试检查您的 log/development.log 文件以了解发生了什么.

If the only method that "works" is update_column my guess is that you have a callback somewhere that is throwing an error. Try to check your log/development.log file to see what's going on.

您也可以使用update_attributes!.这个变体会抛出一个错误,所以它可能会告诉你为什么你的模型没有保存.

You can also use update_attributes!. This variant will throw an error, so it may give you information on why your model isn't saving.

除非您确切地知道自己在做什么,否则您应该使用 update_attributes 并避免使用其他两种方法.如果您稍后向模型添加验证和回调,使用 update_attributeupdate_column 会导致非常难以调试的令人讨厌的行为.

You should use update_attributes and avoid the two other methods unless you know exactly what you are doing. If you add validations and callbacks later to your model, using update_attribute and update_column can lead to nasty behaviour that is really difficult to debug.

您可以查看此链接了解更多信息.

You can check this link for more info on that.

这篇关于Rails:update_column 有效,但 update_attributes 无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆