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
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_recordonbefore_enter.The issue is that even when for some reason
generate_new_log_recordis failed to create a new record, thestateis still changing tofilled. How can we make sure it happens inside a single DBtransactionso that when thegenerate_new_log_recordfails it undo every change that happened?Thank you