문제

내 모델에서 after_update 콜백이 있습니다

after_update :do_something
.

그러나 객체가 생성 된 후 메소드를 호출하지 않으려 고하지 않습니다.After_Update에 면제를 추가하는 것이 있습니까?

도움이 되었습니까?

해결책

Rails에는 많은 다른 콜백 개체 after_createafter_save가 호출됩니다.기존 오브젝트를 업데이트하면 after_updateafter_save가 호출됩니다.

after_createafter_update가 기존 오브젝트를 업데이트하기 만하면서 만 생성시에만 사용됩니다.after_save는 적절한 작성 / 업데이트 콜백 후 두 경우 모두에서 실행됩니다.

오브젝트가 생성 된 후에 만 메소드를 호출하지 않으려면 after_create를 대신 사용해야합니다.

필요한만큼의 콜백을 정의 할 수 있습니다

class User < ActiveRecord::Base
  after_create :one_method
  after_create :another_method
  after_update :one_more_method_only_for_updates

  def one_method
  end

  def another_method
  end

  def one_more_method_only_for_updates
  end
end
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top