before_remove to abort remove在抛出异常时崩溃服务器

时间:2017-01-19 13:30:25

标签: ruby-on-rails activerecord rails-activerecord ruby-on-rails-5

我正在使用Rails 5的协会回调。我在文档中读到,如果要从关联中删除对象,则必须抛出异常:

  

如果before_add回调引发异常,则该对象不会添加到集合中。同样,如果before_remove回调引发异常,则不会从集合中删除该对象。

我正是这样做但是服务器崩溃了:

UncaughtThrowError (uncaught throw :abort)

这就是我的代码的样子:

def can_remove_association(object)
  return unless object.condition
  errors[:base] << "Can't be removed."
  throw(:abort)
end

有什么线索是怎么回事? throw(:abort)避免了要移除的对象,但是我得到了一个难看的500!

一些参考:http://blog.bigbinary.com/2016/02/13/rails-5-does-not-halt-callback-chain-when-false-is-returned.html

1 个答案:

答案 0 :(得分:1)

是的,您需要使用

progressDialog.setMessage("Registering User...");
    progressDialog.show();
    firebaseAuth.createUserWithEmailAndPassword(userEmail,userPassword)
            .addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult>task){
                   if(task.isSuccessful()){
                       progressDialog.hide();
                       Toast.makeText(Login.this, "User Registered!", Toast.LENGTH_SHORT).show();
                   }
                    else{
                       Toast.makeText(Login.this,"Could not register user!",Toast.LENGTH_SHORT).show();
                   }
                }
            });

请参阅https://ruby-doc.org/core-2.4.0/Kernel.html#method-i-throw

https://ruby-doc.org/core-2.4.0/Kernel.html#method-i-raise