2016-11-11 76 views

回答

0

一個令人費解的方式是不做驗證就做object.save,然後用update來更新應該驗證的屬性。所有其他驗證都將被跳過,並且只會驗證您所需的屬性:

# save all other attributes other than the one that needs validation 
object.assign_attributes params.except(:required_attribute) 
object.save validate: false 

if object.persisted? 
    # update the attribute which requires validation 
    object.update required_attribute: params[:required_attribute] 

    if ! object.valid? 
    # handle validation error 
    end 
end