I have two models (lets call then A
and B
).
A
has_many
b
s and 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
Other than that, B
has a before_update
callback that sets some_field
to nil if A
has some_enum_value
set.
Since this relation is used on a nested form, that before_update
from B
is only being called if I update a attribute form B
. If I only change a value form A
that callback is not called.
How can I call B
's before_update
when A
is updated?
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…