You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a simple aasm model like the one below. When calling post.fill!(data), it needs to create a new record on a different table with the data value. This works in the below code by calling generate_new_log_record on before_enter.
class Post < ApplicationRecord
belongs_to :parent_model
include AASM
aasm column: :status do
state :posted, initial: true
state :filled, before_enter: Proc.new { |data| generate_new_log_record(data) }
event :fill do
transitions from: [:posted], to: :filled
end
end
def generate_new_log_record(data)
# how to make this inside the event transaction????
parent_model.log_records.create(start_date: expected_start_time, end_date: expected_end_time, data: data)
end
end
The issue is that even when for some reason generate_new_log_record is failed to create a new record, the state is still changing to filled. How can we make sure it happens inside a single DB transaction so that when the generate_new_log_record fails it undo every change that happened?
Thank you
The text was updated successfully, but these errors were encountered:
I have a simple aasm model like the one below. When calling post.fill!(data), it needs to create a new record on a different table with the data value. This works in the below code by calling
generate_new_log_record
onbefore_enter
.The issue is that even when for some reason
generate_new_log_record
is failed to create a new record, thestate
is still changing tofilled
. How can we make sure it happens inside a single DBtransaction
so that when thegenerate_new_log_record
fails it undo every change that happened?Thank you
The text was updated successfully, but these errors were encountered: