Rails:update_column有效,但不是update_attributes

时间:2021-06-28 01:19:59

I have a simple model:

我有一个简单的模型:

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

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:

这不会引发错误,但它实际上也不会更新回复。相反,我得到了“我正在尝试更新!”消息,就像一切都有效。但是当我重新加载页面并查看回复时,它具有相同的文本。它实际上还没有更新。如果我用以下内容替换update_attributes:

@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)

3 个解决方案

#1


64  

The three methods you are using do different things:

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

  • update_attributes tries to validate the record, calls callbacks and saves;
  • update_attributes尝试验证记录,调用回调和保存;
  • update_attribute doesn't validate the record, calls callbacks and saves;
  • update_attribute不验证记录,调用回调和保存;
  • 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不验证记录,不调用回调,不调用save方法,虽然它确实更新了数据库中的记录。

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_column,我的猜测是你在某处发生了一个错误的回调。尝试检查您的log / development.log文件,看看发生了什么。

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!。此变体将引发错误,因此它可能会为您提供有关模型未保存的原因的信息。

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.

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

You can check this link for more info on that.

您可以查看此链接以获取更多相关信息。

#2


0  

I had this same issue, but with Rails 4. The issue happens when you have params[] in update_attribute. In Rails 4 with strong parameters

我有同样的问题,但是使用Rails 4.当你在update_attribute中有params []时会出现问题。在Rails 4中具有很强的参数

@reply.update_attributes(params[reply_params])

should be

应该

@reply.update_attributes(reply_params)

I'm not to familiar with Rails 3 but this should be the issue:

我不熟悉Rails 3,但这应该是问题所在:

@reply.update_attributes(params[:reply])

should be

应该

@reply.update_attributes(:reply)

#3


0  

A gut guess would be to say that you have a mass assignment problem and should add your attributes in your model like this

一个猜测就是说你有一个质量分配问题,应该在你的模型中添加你的属性

attr_accessible: :your_attribute, :your_attribute2

#1


64  

The three methods you are using do different things:

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

  • update_attributes tries to validate the record, calls callbacks and saves;
  • update_attributes尝试验证记录,调用回调和保存;
  • update_attribute doesn't validate the record, calls callbacks and saves;
  • update_attribute不验证记录,调用回调和保存;
  • 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不验证记录,不调用回调,不调用save方法,虽然它确实更新了数据库中的记录。

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_column,我的猜测是你在某处发生了一个错误的回调。尝试检查您的log / development.log文件,看看发生了什么。

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!。此变体将引发错误,因此它可能会为您提供有关模型未保存的原因的信息。

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.

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

You can check this link for more info on that.

您可以查看此链接以获取更多相关信息。

#2


0  

I had this same issue, but with Rails 4. The issue happens when you have params[] in update_attribute. In Rails 4 with strong parameters

我有同样的问题,但是使用Rails 4.当你在update_attribute中有params []时会出现问题。在Rails 4中具有很强的参数

@reply.update_attributes(params[reply_params])

should be

应该

@reply.update_attributes(reply_params)

I'm not to familiar with Rails 3 but this should be the issue:

我不熟悉Rails 3,但这应该是问题所在:

@reply.update_attributes(params[:reply])

should be

应该

@reply.update_attributes(:reply)

#3


0  

A gut guess would be to say that you have a mass assignment problem and should add your attributes in your model like this

一个猜测就是说你有一个质量分配问题,应该在你的模型中添加你的属性

attr_accessible: :your_attribute, :your_attribute2