LoginSignup
2
1

More than 5 years have passed since last update.

after_create, after_update, after_destroyというようなafter_xxxのcallbackを止めたいとき

Posted at

起きたこと

after_createに定義しているメソッドを止める。

ex)

after_create :call_name

def call_name
  return throw(:abort) unless watasan.present?
end

uncaught throw abort error が発生。

対応方法

after_xxx をとめるときは raise ActiveRecord::Rollback を使う

after_create :call_name

def call_name
  raise ActiveRecord::Rollback unless watasan.present?
end

* Rollbackはさせたくない場合は他の例外を渡してあげるとその例外を吐いてくれます。

雑談

before_xxx を止めたいときは throw(:abort)を明示する必要があります。

参考

https://github.com/rails/rails/pull/8479#issuecomment-46124455
https://blog.toshimaru.net/active-record-callbacks/

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1