두 모델이 있습니다 (A
및 B
을 호출 할 수 있습니다).레일스는 중첩 된 특성을 가진 콜백 before_update를 호출합니다.
A
has_many
b
들과 B
belongs_to
A
. 그 외에는
class A < ApplicationRecord
has_many :bs, dependent: :destroy, inverse_of: :a
accepts_nested_attributes_for :bs, reject_if: :all_blank, allow_destroy: true
validates_associated :bs
end
class B < ApplicationRecord
belongs_to :a, inverse_of: :bs
before_update :do_something, unless: Proc.new { |b| b.a.some_enum_value? if a }
def do_something
self.some_field = nil
end
end
는 B
는 A
가 some_enum_value
세트가 some_field
닐 경우에 설정하는 before_update
콜백을 갖는다. 나는 속성 양식 B
를 업데이트 할 경우 B
에서 before_update
만 호출되고 있음이 관계가 중첩 된 형태에 사용되기 때문에
. 값 양식 A
만 변경하면 콜백이 호출되지 않습니다.
A
을 업데이트하면 어떻게 B
의 before_update
으로 전화 할 수 있습니까?
미리 감사드립니다. 잠재적 재앙 성능 결과를 초래할 수 있기 때문에 (그러나이 옵션을 has_many
관계가 존재하지 않습니다
class B < ApplicationRecord
belongs_to :a, inverse_of: :bs, touch: true
end
당신이 B. 를 업데이트 할 때 a.updated_at
을 업데이트 : 협회에 속한 들어
당신이 이런 일을 할 때 정말주의해야 스케일링 관점에서. 쓰기 작업은 비용이 많이 들고 일반적으로 프록시를 사용하거나 조인을 사용하여이를 해결할 수 있습니다. – max
고맙습니다. 맥스. – ThalesMiguel