简体   繁体   English

Rails 3 - 在更新之前检查该对象是否对params有效

[英]Rails 3 - check that object is valid with params before update

I have very newbie question. 我有一个非常新手的问题。 How can i check that object of model is valid with new params BEFORE updating it? 在更新之前,如何检查模型对象是否对新参数有效?

I want transform that: 我希望改造:

def update
  @obj = SomeModel.find( params[:id] )

  if @obj.update_attributes( params[:obj] )
    # That have been updated
  else
    # Ups, errors!
  end
end

To something like that: 对于这样的事情:

def update
  @obj = SomeModel.find( params[:id] )

  if @obj.valid_with_new_params( params[:obj] )
    @obj.update_attributes( params[:obj] )
  else
    # Ups, errors!
  end
end

To update the attributes without saving them, you can use 要更新属性而不保存它们,您可以使用

@obj.assign_attributes( params[:obj] )

Then to check if the object is valid, you can call 然后检查对象是否有效,你可以打电话

@obj.valid?

If the object is not valid, you can see the errors (only after calling .valid? ) by calling 如果对象是无效的,你可以看到错误(只打完电话后.valid?通过调用)

@obj.errors

If the object is valid, you can save it by calling 如果对象有效的,你可以通过调用保存

@obj.save

However, all of this usually isn't necessary. 但是,所有这一切通常都没有必要。 If the object isn't valid, then ActiveRecord won't save the object to the database, so all of the attribute changes are forgotten when you leave the controller action. 如果对象无效,则ActiveRecord不会将对象保存到数据库,因此在您离开控制器操作时会忘记所有属性更改。

Also, since an invalid record won't be saved to the database, you can always just call Object.find() again to get the original object back. 此外,由于无效记录将不会保存到数据库,因此您始终可以再次调用Object.find()以获取原始对象。

You can call the valid? 你可以打电话valid? method to run the validations. 运行验证的方法。

This doesn't guarantee that a subsequent save will succeed if some of your validations depend on the state of other objects in the database. 如果某些验证取决于数据库中其他对象的状态,则不保证后续保存将成功。 Te save could also fail for reasons unconnected to validations (eg a foreign key constraint) 由于未连接到验证的原因(例如外键约束),Te save也可能失败

I'm not sure why you'd want this pattern 我不确定你为什么要这种模式

The object won't be saved if the passed argument doesn't produce a valid object, so you can use your way just fine. 如果传递的参数不产生有效对象,则不会保存该对象,因此您可以正常使用。 You can see the errors (if any) using the @obj.errors array. 您可以使用@obj.errors数组查看错误(如果有)。

update_attributes method validate object and return false if object is invalid. update_attributes方法验证对象,如果对象无效,则返回false。 So, you can just write: 所以,你可以写:

if @obj.update_attributes( params[:obj] )
  # That have been update
else
  # Ups, errors!
end

The answer is that you can define a method 答案是你可以定义一个方法

def valid_with_new_params(hash)
   self.attributes = hash
   valid?
end

But that would be unnecessary because @obj.update_attributes(params[:obj]) returns true if the obj was successfully updated and false otherwise. 但这是不必要的,因为@obj.update_attributes(params[:obj])如果obj成功更新则返回true否则返回false Note also that internally the update_attributes method runs all validations on the @obj so that you have @obj.errors available if the update failed. 另请注意, update_attributes方法在内部运行@obj上的所有验证,以便在更新失败时可以使用@obj.errors

To update the attributes without saving them 更新属性而不保存它们

@obj.attributes = params[:obj]   0r
@obj.attributes = {:name => “Rob”}

To then check if the object is valid 然后检查对象是否有效

@obj.valid?

To check if there is any error 检查是否有任何错误

@obj.errors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM