Rails:如何检查“update_attributes”是否可用会失败吗?

发布于 2024-10-12 03:02:02 字数 493 浏览 1 评论 0原文

要检查 buyer.save 是否会失败,我使用 buyer.valid?

def create
  @buyer = Buyer.new(params[:buyer])
  if @buyer.valid?
    my_update_database_method
    @buyer.save
  else
    ...
  end
end

如何检查 update_attributes 是否会失败?

def update 
  @buyer = Buyer.find(params[:id])
  if <what should be here?>
    my_update_database_method
    @buyer.update_attributes(params[:buyer])
  else
    ...
  end
end

To check if buyer.save is going to fail I use buyer.valid?:

def create
  @buyer = Buyer.new(params[:buyer])
  if @buyer.valid?
    my_update_database_method
    @buyer.save
  else
    ...
  end
end

How could I check if update_attributes is going to fail ?

def update 
  @buyer = Buyer.find(params[:id])
  if <what should be here?>
    my_update_database_method
    @buyer.update_attributes(params[:buyer])
  else
    ...
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北渚 2024-10-19 03:02:02

如果未完成,则返回 false,与 save 相同。如果您更喜欢的话,save! 会抛出异常。我不确定是否有 update_attributes!,但这是合乎逻辑的。

只需

if @foo.update_attributes(params)
  # life is good
else
  # something is wrong
end

http://apidock.com/rails/ActiveRecord/Base/update_attributes

编辑

然后你想要这个方法你就必须写。如果你想预先检查参数卫生。

def params_are_sanitary?
  # return true if and only if all our checks are met
  # else return false
end

编辑2

或者,根据您的限制

if Foo.new(params).valid? # Only works on Creates, not Updates
  @foo.update_attributes(params)
else
  # it won't be valid.
end

it returns false if it was not done, same with save. save! will throw exceptions if you like that better. I'm not sure if there is update_attributes!, but it would be logical.

just do

if @foo.update_attributes(params)
  # life is good
else
  # something is wrong
end

http://apidock.com/rails/ActiveRecord/Base/update_attributes

Edit

Then you want this method you have to write. If you want to pre check params sanitation.

def params_are_sanitary?
  # return true if and only if all our checks are met
  # else return false
end

Edit 2

Alternatively, depending on your constraints

if Foo.new(params).valid? # Only works on Creates, not Updates
  @foo.update_attributes(params)
else
  # it won't be valid.
end
回梦 2024-10-19 03:02:02

如果对象无效,则 update_attributes 方法返回 false。因此,只需使用此构造

def update
  if @buyer.update_attributes(param[:buyer])
    my_update_database_method
  else
    ...
  end
end

如果您的 my_update_database_method 必须仅在 update_attributes 之前调用,那么您应该使用合并方式,可能如下所示:

def update
  @buyer = Buyer.find(params[:id])
  @buyer.merge(params[:buyer])
  if @buyer.valid?
    my_update_database_method
    @buyer.save
  else
    ...
  end
end

The method update_attributes returns false if object is invalid. So just use this construction

def update
  if @buyer.update_attributes(param[:buyer])
    my_update_database_method
  else
    ...
  end
end

If your my_update_database_method has to be call only before update_attributes, then you shoud use merge way, probably like this:

def update
  @buyer = Buyer.find(params[:id])
  @buyer.merge(params[:buyer])
  if @buyer.valid?
    my_update_database_method
    @buyer.save
  else
    ...
  end
end
疏忽 2024-10-19 03:02:02

这可能不是最好的答案,但它似乎回答了你的问题。

def self.validate_before_update(buyer)#parameters AKA Buyer.validate_before_update(params[:buyer])
# creates temporary buyer which will be filled with parameters
# the temporary buyer is then check to see if valid, if valid returns fail.
      temp_buyer = Buyer.new
# populate temporary buyer object with data from parameters
      temp_buyer.name = buyer["name"]
# fill other required parameters with valid data
      temp_buyer.description = "filler desc"
      temp_buyer.id = 999999 
# if the temp_buyer is not valid with the provided parameters, validation fails
    if  temp_buyer.valid? == false
        temp_buyer.errors.full_messages.each do |msg|
          logger.info msg
        end        
# Return false or temp_buyer.errors depending on your need.
        return false
    end

return true

结尾

This may not be the best answer, but it seems to answer your question.

def self.validate_before_update(buyer)#parameters AKA Buyer.validate_before_update(params[:buyer])
# creates temporary buyer which will be filled with parameters
# the temporary buyer is then check to see if valid, if valid returns fail.
      temp_buyer = Buyer.new
# populate temporary buyer object with data from parameters
      temp_buyer.name = buyer["name"]
# fill other required parameters with valid data
      temp_buyer.description = "filler desc"
      temp_buyer.id = 999999 
# if the temp_buyer is not valid with the provided parameters, validation fails
    if  temp_buyer.valid? == false
        temp_buyer.errors.full_messages.each do |msg|
          logger.info msg
        end        
# Return false or temp_buyer.errors depending on your need.
        return false
    end

return true

end

<逆流佳人身旁 2024-10-19 03:02:02

你最好通过 before_save 在你的模型中检查它

before_save :ensure_is_valid
private 
def ensure_is_valid
  if self.valid?
  else
  end
end

you'd better check it in your model through a before_save

before_save :ensure_is_valid
private 
def ensure_is_valid
  if self.valid?
  else
  end
end
触ぅ动初心 2024-10-19 03:02:02

我遇到了同样的情况 - 需要知道记录是否有效并在更新保存之前执行一些操作。我发现有 assign_attributes(attributes) 方法 < code>update 方法在 save 之前使用。所以现在这样做可能是正确的:

def update
  @buyer = Buyer.find(params[:id])
  @buyer.assign_attributes(params[:buyer])
  if @buyer.valid?
    my_update_database_method
    @buyer.save
  else
    ...
  end
end

I've run into the same scenario - needed to know if record is valid and do some actions before update save. I've found out that there is assign_attributes(attributes) method which update method uses before save. So nowadays it's likely correct to do:

def update
  @buyer = Buyer.find(params[:id])
  @buyer.assign_attributes(params[:buyer])
  if @buyer.valid?
    my_update_database_method
    @buyer.save
  else
    ...
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文