I have a method in model that calls after create
after_create :generate_insurer_recovery_invoice, if: :insurance_recovery_batch?
How should I write another condition within this callback?
I have a method in model that calls after create
after_create :generate_insurer_recovery_invoice, if: :insurance_recovery_batch?
How should I write another condition within this callback?
You can also do this for a shorter readable version
after_save :update_offices_people_count, if: -> {office_id_changed? || trashed_changed?}
P.S: ->
is a shorthand version of writing lambda
.
after_save :update_offices_people_count, if: -> {office_id_changed? || trashed_changed?}
–
Trail I think this may be usefull to you
You can achive this something like this, from the following post
Multiple conditions on callbacks
after_save :update_offices_people_count
private
def update_offices_people_count
if office_id_changed? || trashed_changed?
...
end
end
The lambda
or proc
methods work well but you can also just pass in an Array
to if
, like so:
after_create :generate_insurer_recovery_invoice, if: [ :insurance_recovery_batch?, :active_insurer? ]
I'm not sure what version of Rails this was added but take a look at the Rails 7 docs here:
https://guides.rubyonrails.org/active_record_callbacks.html#multiple-callback-conditions
© 2022 - 2024 — McMap. All rights reserved.